简体   繁体   English

由'form'元素引起的JS异常

[英]A JS exception caused by 'form' element

Today I ran into this problem and it annoys me a lot... I'm maintaining a JS project and there's a line: 今天我遇到了这个问题,它让我很烦恼......我正在维护一个JS项目并且有一条线:

node.tagName

where node.nodeType is 1 . 其中node.nodeType1 Obviously the code wants to get the tag name of this element and it seems to work fine for 99.99% webpages... 显然,代码想要获取此元素的标记名称,它似乎适用于99.99%的网页...

Sadly, when I execute the script on http://codeforces.com/problemset/problem/377/D , it doesn't work. 可悲的是,当我在http://codeforces.com/problemset/problem/377/D上执行脚本时,它不起作用。 The reason is that there's a form element with a child node which has property name="tagName" . 原因是有一个带有子节点的form元素,其属性name="tagName"
在此输入图像描述

Child nodes of form element who have property name can be accessed like: node.NAME_VALUE ( reference ), so node.tagName will get its child whose name property is tagName rather than the node's tag name form . 具有属性nameform元素的子节点可以像: node.NAME_VALUEreference )一样访问,因此node.tagName将获取其name属性为tagName而不是节点的标记名称form的子节点。

Does anyone ever run into this problem, too? 有没有人遇到过这个问题? Are there any other solutions except checking whether the node is form ? 除了检查节点是否是form之外,还有其他解决方案吗?


EDIT 1: 编辑1:
I've fired a bug for jQuery here . 我在这里为jQuery解决了一个bug。 I did this because it may be much easier for jQuery to fix this for .prop("tagName") than making all browsers solve this issue. 我这样做是因为jQuery可能更容易为.prop("tagName")修复此问题, .prop("tagName")不是让所有浏览器解决此问题。
BTW, I think no one should use something like tagName or nodeName as an HTMLInputElement's name value. 顺便说一下,我认为没有人应该使用像tagNamenodeName这样的东西作为HTMLInputElement的name值。

I'm no expert on these things, so this may not be the best answer you could get, but never the less... i can think of two solutions: 我不是这些东西的专家,所以这可能不是你能得到的最好的答案,但从来没有......我能想到两个解决方案:

1) Use nodeName instead if you can be certain that input name will not exist. 1)如果您可以确定输入名称不存在,请使用nodeName。

2) Use element.clone(false).tagName (such as document.getElementById('someid').clone(false).tagName ) 2)使用element.clone(false).tagName (例如document.getElementById('someid').clone(false).tagName

If node.tagName does not work for you, use node.nodeName . 如果node.tagName不适合您,请使用node.nodeName

I use node.nodeName in my code. 我在我的代码中使用node.nodeName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM