跨浏览器,javascript getAttribute() 方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3755227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cross-browser, javascript getAttribute() method?
提问by tester
trying to determine a decent, cross browser method for obtaining attributes with javascript? assume javascript library use (jQuery/Mootools/etc.) is not an option.
试图确定一种使用 javascript 获取属性的体面的跨浏览器方法?假设 javascript 库使用(jQuery/Mootools/等)不是一个选项。
I've tried the following, but I frequently get "attributes" is null or not an object error when IE tries to use the "else" method. Can anyone assist?
我尝试了以下方法,但是当 IE 尝试使用“else”方法时,我经常得到“属性”为空或不是对象错误。任何人都可以提供帮助吗?
<script type="text/javascript">
//...
getAttr: function(ele, attr) {
if (typeof ele.attributes[attr] == 'undefined'){
return ele.getAttribute(attr);
} else {
return ele.attributes[attr].nodeValue;
}
},
//...
</script>
<div>
<a href="http://www.yo.com#foo">Link</a>
</div>
using the above html, in each browser, how do I getAttr(ele, 'href')? (assume selecting the ele node isn't an issue)
使用上面的 html,在每个浏览器中,我如何 getAttr(ele, 'href')?(假设选择 ele 节点不是问题)
采纳答案by user113716
With regard to your question's update, you could try this.
关于你的问题的更新,你可以试试这个。
It may be overkill, but if getAttribute()
and the dot notation don't return a result, it iterates through the attributes
object to try to find a match.
这可能getAttribute()
有点矫枉过正,但如果点符号没有返回结果,它会遍历attributes
对象以尝试找到匹配项。
Example:http://jsfiddle.net/4ZwNs/
示例:http : //jsfiddle.net/4ZwNs/
var funcs = {
getAttr: function(ele, attr) {
var result = (ele.getAttribute && ele.getAttribute(attr)) || null;
if( !result ) {
var attrs = ele.attributes;
var length = attrs.length;
for(var i = 0; i < length; i++)
if(attrs[i].nodeName === attr)
result = attrs[i].nodeValue;
}
return result;
}
};
var result = funcs.getAttr(el, 'hash');
It's up to you to do some cross-browser testing, though. :o)
不过,这取决于您进行一些跨浏览器测试。:o)
Using ele.attributes
, you need to access them by index, as in:
使用ele.attributes
,您需要通过索引访问它们,如下所示:
ele.attributes[0].nodeName; // "id" (for example)
ele.attributes[0].nodeValue; // "my_id" (for example)
Trying to pass attributes
an attribute name appears to return a value whose typeof
is object
, so your else
code is running even though ele.attributes[attr]
doesn't give you the value you want.
试图通过attributes
属性名称似乎其返回值typeof
是object
,让你的else
代码运行,即使ele.attributes[attr]
不给你你想要的值。
回答by Mark Rhodes
For the vast majority of cases you can simply use the built in getAttribute
function.
对于绝大多数情况,您可以简单地使用内置getAttribute
函数。
e.g.
例如
ele.getAttribute(attr)
According to QuirksModethis should work on all major browsers (IE >= 6 included), with a minor exception:
根据QuirksMode,这应该适用于所有主要浏览器(包括 IE >= 6),但有一个小例外:
In IE5-7, accessing the style
attribute gives an object, and accessing the onclick
attribute gives an anonymous function wrapped around the actual content.
在 IE5-7 中,访问该style
属性会给出一个对象,而访问该onclick
属性会给出一个包裹在实际内容周围的匿名函数。
回答by Robusto
You are trying to access properties of ele before you've established if those properties exist. Try this kind of evidence chain:
在确定这些属性是否存在之前,您正在尝试访问 ele 的属性。试试这种证据链:
if (ele.attributes && ele.attributes[attr] && typeof ele.attributes[attr] == 'undefined')
etc.
等等。