我需要製作一個與IE7兼容的現有Web應用程序。JavaScript:如果未定義,實現'element.hasAttribute'[用於IE7]
該代碼廣泛使用element.hasAttribute
,IE7對此方法有問題。
對象不支持屬性或方法「hasattribute」
我想在代碼中檢查是否有input
元素定義了hasAttribute
方法,如果沒有,我想它添加到所有input
元素。
//create an input element variable << works fine
var myInput = document.createElement("input");
//see if it has the 'hasAttribute' method << condition works fine
if (('hasAttribute' in myInput)==false)
{
//get all input elements into objInputElements <<works fine
var objInputElements=document.getElementsByTagName("input");
// MORE CODE NEEDED - To implement a hasAttribute function for all
// elements in the array probably using something
// like: !!element[attributeName] which works in IE7. See link and notes below.
}
This article介紹瞭如何定義一個單獨的函數來做到這一點。但是,如果未定義元素,我想將hasattribute
方法添加到元素中。 (這種方式我不需要更改當前寫入的所有代碼)
重要提示:在表單中有> 1000個隱藏輸入字段,因此'hasattribute'方法需要添加到元素中一個非常有效的方法。
請讓我知道我如何實現這一目標。謝謝!
環比'node.attributes'或檢查''對null' getAttribute'。 –
你可以試試if(!Element.prototype.hasAttribute)Element.prototype.hasAttribute = function(){...'不知道IE7如何對此做出反應。 – bfavaretto
@bfavaretto,不支持IE7中的Element.prototype – epascarello