Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/411.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript:实现&x27;元素。hasAttribute';如果未定义[适用于IE7]_Javascript_Html_Dom_Internet Explorer 7_Extend - Fatal编程技术网

JavaScript:实现&x27;元素。hasAttribute';如果未定义[适用于IE7]

JavaScript:实现&x27;元素。hasAttribute';如果未定义[适用于IE7],javascript,html,dom,internet-explorer-7,extend,Javascript,Html,Dom,Internet Explorer 7,Extend,我需要使现有的web应用程序与IE7兼容 代码广泛使用元素.hasAttribute,IE7对此方法存在问题 对象不支持属性或方法“hasattribute” 我试图在代码中检查input元素是否定义了hasaAttribute方法,如果没有,我试图将其添加到所有input元素中 //create an input element variable << works fine var myInput = document.createElement("input"); //see

我需要使现有的web应用程序与IE7兼容

代码广泛使用
元素.hasAttribute
,IE7对此方法存在问题

对象不支持属性或方法“hasattribute”

我试图在代码中检查
input
元素是否定义了
hasaAttribute
方法,如果没有,我试图将其添加到所有
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.
}

//创建输入元素变量,因为IE<8不支持
元素.prototype
,所以没有有效的方法来polyfill
hasAttribute
。效率低下的方法(如果您希望避免填隙)如下所示(在加载所有输入后放置):


如果(!window.Element | | |!window.Element.prototype | |!window.Element.prototype.hasAttribute){
(功能(){
函数hasAttribute(属性名){
return typeof this[attrName]!==“undefined”;//您还可以检查getAttribute()是否为null,尽管这可能会导致任何遵循旧DOM3方法返回空字符串的空字符串的旧浏览器(如果有的话)出现问题(但根据我们上面的检查,没有拥有hasAttribute)看见https://developer.mozilla.org/en-US/docs/Web/API/Element.getAttribute
}
var inputs=document.getElementsByTagName('input');
对于(变量i=0;i
我知道这是一篇老文章,也许没有其他人使用IE7,但如果你像我一样需要它(并且需要使用ajax或类似的东西),这就是我的建议

也许我们可以通过创建
getElementsByTagName
getElementById
的代理来提高性能,从而增加对运行时创建的动态元素的支持

也许是这样的:

if (!window.Element || !window.Element.prototype || !window.Element.prototype.hasAttribute) {
   (function (document) {
      var originalGetElementById = document.getElementById;
      var originalGetElementsByTagName = document.getElementsByTagName;

      // The HasAttribute function.
      function hasAttribute (attrName) {
         return typeof this[attrName] !== 'undefined';
      }

      // Add the HasAttribute to the element if is not present yet.
      function attachFunction (element) {
         if (element && !element.hasAttribute) {
            element.hasAttribute = hasAttribute;
         }
         return element;
      }

      // Proxy of the document.getElementById
      document.getElementById = function (elementId) {
         var element = originalGetElementById(elementId);
         return attachFunction(element);
      }

      // Proxy of the document.getElementsByTagName
      document.originalGetElementsByTagName = function (tagName) {
         var elements = originalGetElementsByTagName(tagName);
         for(var i = 0, len = elements.length; i < len; i++) {
            attachFunction(element[i]);
         }
         return elements;
      }
   }(document));
}
试试看:

//if  po is object then for IE:
if (!po.hasAttribute) po.hasAttribute=function(attr) {
  return this.getAttribute(attr)!=null
};

循环遍历
节点.attributes
或对照
null
检查
getAttribute
。如果(!Element.prototype.hasAttribute)Element.prototype.hasAttribute=function(),可以尝试
{…
不确定IE7将如何对此作出反应。@bfavaretto,IE7
节点中不支持Element.prototype。IE7中也不支持attributes
getAttribute
根据旧的DOM3规范和一些DOM实现(但不是IE)返回空字符串,而不是null如果
返回this.getAttributeNode(attrName).specified;
,这不是更好吗?我发现了一个问题:
getAttributeNode
为不存在的非DOM属性返回null,因此它必须是
var an=this.getAttributeNode(attrName);返回(an==null)?false:an.specified;
您可以只使用
getAttribute
,特别是因为它不再指定为空时返回空字符串(而且显然从来没有遵循过旧规范),因此它将返回
null
,从而可以区分缺失和空。很抱歉,我只是很快读了它的注释“IE 8或更高版本”。你是对的,被弃用其实并不重要,但如果linting程序可能会提醒你这一点,那会让你感到恼火。当属性不存在时,IE7将返回一个非空值?在某些情况下,是的,例如
i
中的
标记上的
i
将返回一个值(非空)即使属性不存在。
<!--[if lt IE 8]>
<script src="ie7fixs.js" type="text/javascript" ></script>
<![endif]-->
var inputs = document.getElementsByTagName('input');
document.write(
    'has?' + inputs[0].hasAttribute('abc') // false
);
document.write(
    'has?' + inputs[0].hasAttribute('data-abc') // true
);
//if  po is object then for IE:
if (!po.hasAttribute) po.hasAttribute=function(attr) {
  return this.getAttribute(attr)!=null
};