简体   繁体   English

跨浏览器,javascript getAttribute()方法?

[英]Cross-browser, javascript getAttribute() method?

trying to determine a decent, cross browser method for obtaining attributes with javascript? 试图确定一个体面的,跨浏览器的方法来获取javascript属性? assume javascript library use (jQuery/Mootools/etc.) is not an option. 假设javascript库使用(jQuery / Mootools / etc。)不是一个选项。

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. 我已经尝试了以下内容,但是当IE尝试使用“else”方法时,我经常得到“属性”为null或者不是对象错误。 Can anyone assist? 有人可以帮忙吗?

<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' )? 使用上面的html,在每个浏览器中,我如何getAttr(ele, 'href' )? (assume selecting the ele node isn't an issue) (假设选择ele节点不是问题)

For the vast majority of cases you can simply use the built in getAttribute function. 对于绝大多数情况,您可以简单地使用内置的getAttribute函数。

eg 例如

ele.getAttribute(attr)

According to QuirksMode this 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属性会给出一个围绕实际内容的匿名函数。

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名称似乎返回一个typeofobject的值,因此即使ele.attributes[attr]没有为您提供所需的值,也会运行else代码。

You are trying to access properties of ele before you've established if those properties exist. 如果存在这些属性,您将尝试在建立之前访问ele的属性。 Try this kind of evidence chain: 试试这种证据链:

if (ele.attributes && ele.attributes[attr] && typeof ele.attributes[attr] == 'undefined')

etc. 等等

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

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