2012-02-10 79 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) { 
... 
}