Fancy Front End Fancy Front End
  • 开始上手
  • 基础
  • 调度器(Scheduler)
  • 更新器(Updater)
  • 渲染器(Render)
  • 更新周期
  • hooks 原理
  • 总结
  • 📙 React源码漂流记
  • 开始上手
  • 基础
  • reactivity
  • runtime-core
  • runtime-dom
  • Awesome Web
  • Awesome NodeJS
话题
  • 导航
  • Q&A
  • 幻灯片
  • 关于
  • 分类
  • 标签
  • 归档
博客 (opens new window)
GitHub (opens new window)

Jonsam NG

让有意义的事变得有意思,让有意思的事变得有意义
  • 开始上手
  • 基础
  • 调度器(Scheduler)
  • 更新器(Updater)
  • 渲染器(Render)
  • 更新周期
  • hooks 原理
  • 总结
  • 📙 React源码漂流记
  • 开始上手
  • 基础
  • reactivity
  • runtime-core
  • runtime-dom
  • Awesome Web
  • Awesome NodeJS
话题
  • 导航
  • Q&A
  • 幻灯片
  • 关于
  • 分类
  • 标签
  • 归档
博客 (opens new window)
GitHub (opens new window)
  • 开始上手
  • Plan 计划
  • typescript-utility

  • single-spa源码

  • qiankun源码

  • webpack

  • axios

  • solid

  • vite源码

  • jquery源码

    • 开始阅读
    • JQuery 源码分析
    • JQuery 源码注释
    • jQuery 源码问题扩展
      • IE和Opera会用name代替ID返回元素的问题
  • snabbdom

  • am-editor

  • html2canvas

  • express

  • acorn源码

  • immutable.js源码

  • web
  • jquery源码
jonsam
2022-04-14
目录

jQuery 源码问题扩展

# IE 和 Opera 会用 name 代替 ID 返回元素的问题

IE works correctly on elements that CAN'T have a name attribute. It works incorrectly, however, on elements that can.

As I researched this, I found a function in JavaScript I've never used before:

HTMLCollection.namedItem( String name )

This function seeks for any element within the collection whose id attribute value matches the name specified in the call. If it doesn't find one, it searches for any element whose name attribute matches the name specified in the call. The function returns the first element object it finds that meets the criteria, or null if it can't find one.

My first thought was, Microsoft has their document.getElementById calling their namedItem() function. But then I stepped back and realized through my experiment that IE seems to be matching against both name and id at the same time to match the value.

As you'll notice below, I have two elements for each part of the experiment. The first has the name attribute filled with the id value "my[Element]" I'm wanting to look for. I fill the second element's id attribute with the same value. IE, parsing from the top of the document down, finds the element with the name="my[Element]" first and automatically takes it... it doesn't even wait to see if it can find one with an id that matches first.

I can only assume this was done for optimizing this "convenience feature", but makes for some serious puzzlement and aggravation in troubleshooting why the element a developer can't get to who happens to name/id things in a way similar to what happens in my experiment. Whatever reason, they did it, it's wrong. Please be careful in how you name/id things

jQuery 通过 $().find(selector) 通过查找子节点来解决这个问题的。

参考:

  • Internet Explorer (IE) JavaScript document.getElementById whackedness demo (opens new window)
编辑 (opens new window)
上次更新: 2022/04/28, 23:58:26
JQuery 源码注释
开始阅读

← JQuery 源码注释 开始阅读→

最近更新
01
渲染原理之组件结构与 JSX 编译
09-07
02
计划跟踪
09-06
03
开始上手
09-06
更多文章>
Theme by Vdoing | Copyright © 2022-2022 Fancy Front End | Made by Jonsam by ❤
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式