2012-02-10 76 views
1

我已经编写了一个jQuery代码来从HTML中的TR标签获取属性。该代码适用于IE 8,但是当我在IE 9中运行该代码时,出现“预期的功能”错误。代码如下:Jquery - 在IE 9中从TR html标签获取属性

$(".grid tr").each(function() { 

    //I've inserted an attribute called idRow in each grid row. 
    if(this.attributes("idRow") != null) //I get the "Function expected error" here 
    { 
     ... 
    } 

}); 

什么是代码问题?为什么它在IE 8中工作?

回答

1

采取这种方法,你需要...

this.attributes.getNamedItem('idRow').nodeValue 

或...

this.attributes.item('idRow').nodeValue 

...虽然,我建议只使用getAttribute()

this.getAttribute('idRow') 
+0

谢谢!你知道我的代码为什么在IE 8中工作吗? – Leila 2012-02-10 19:11:04

+2

@Leila:我猜他们最初有'属性'对象作为可调用的函数/对象,但是他们努力达到更符合标准的要求,移除了这个能力。 – 2012-02-10 19:14:01

+1

有道理......非常感谢! – Leila 2012-02-10 19:15:48

-1

我宁愿使用

$(".grid tr").each(function (tr) { 

    //I've inserted an attribute called idRow in each grid row. 
    if(tr.attributes("idRow") != null) //I get the "Function expected error" here 
    { 
     ... 
    } 

}); 
+0

这是对同一元素的引用。它不会修复错误。 – 2012-02-10 19:10:04

+0

无论您的downvote – Raffaele 2012-02-10 19:23:03

+0

*“既不downvote”*是什么意思?你不明白你为什么被低估? – 2012-02-10 19:32:00

1

如何

if($(this).attr('idRow') != undefined) { 
... 
}