本文介绍了innerHTML 删除 Internet Explorer 中的属性引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在IE中获取DOM节点的innerHTML时,如果属性值中没有空格,IE会去掉周围的引号,如下图:

When you get the innerHTML of a DOM node in IE, if there are no spaces in an attribute value, IE will remove the quotes around it, as demonstrated below:

<html>
    <head>
        <title></title>
    </head>
    <body>
        <div id="div1"><div id="div2"></div></div>
        <script type="text/javascript">
            alert(document.getElementById("div1").innerHTML);
        </script>
    </body>
</html>

在 IE 中,警报将显示为:

In IE, the alert will read:

<DIV id=div2></DIV>

这是一个问题,因为我将此传递给需要有效 XHTML 的处理器,并且所有属性值都必须被引用.有谁知道在 IE 中解决此行为的简单方法?

This is a problem, because I am passing this on to a processor that requires valid XHTML, and all attribute values must be quoted. Does anyone know of an easy way to work around this behavior in IE?

推荐答案

IE innerHTML 确实很烦人.我为它写了这个函数,这可能有帮助吗?它引用属性并将标记名设置为小写.顺便说一句,为了让它更烦人,IE 的 innerHTML 并没有从非标准属性中删除引号.

IE innerHTML is very annoying indeed. I wrote this function for it, which may be helpfull? It quotes attributes and sets tagnames to lowercase. By the way, to make it even more annoying, IE's innerHTML doesn't remove quotes from non standard attributes.

根据评论进行编辑该函数现在处理属性值中的更多字符,并可选择将属性值转换为小写.这个函数现在看起来更丑了;~).如果要向方程添加或删除字符,请编辑 [a-zA-Z.:[]_()&$\%#@!0-9]+[?s+|?>] 部分正则表达式.

Edit based on commentsThe function now processes more characters in attribute values and optionally converts attribute values to lower case. The function looks even more ugly now ;~). If you want to add or remove characters to the equation, edit the [a-zA-Z.:[]_()&$\%#@!0-9]+[?s+|?>] part of the regular expressions.

function ieInnerHTML(obj, convertToLowerCase) {
    var zz = obj.innerHTML ? String(obj.innerHTML) : obj
       ,z  = zz.match(/(<.+[^>])/g);

    if (z) {
     for ( var i=0;i<z.length;(i=i+1) ){
      var y
         ,zSaved = z[i]
         ,attrRE = /=[a-zA-Z.:[]_()&$\%#@!0-9/]+[?s+|?>]/g
      ;

      z[i] = z[i]
              .replace(/([<|</].+?w+).+[^>]/,
                 function(a){return a.toLowerCase();
               });
      y = z[i].match(attrRE);

      if (y){
        var j   = 0
           ,len = y.length
        while(j<len){
          var replaceRE =
               /(=)([a-zA-Z.:[]_()&$\%#@!0-9/]+)?([s+|?>])/g
             ,replacer  = function(){
                  var args = Array.prototype.slice.call(arguments);
                  return '="'+(convertToLowerCase
                          ? args[2].toLowerCase()
                          : args[2])+'"'+args[3];
                };
          z[i] = z[i].replace(y[j],y[j].replace(replaceRE,replacer));
          j+=1;
        }
       }
       zz = zz.replace(zSaved,z[i]);
     }
   }
  return zz;
}

应该起作用的示例键值对

Example key-value pairs that should work

data-mydata=return[somevalue] => data-mydata="return[somevalue]"
id=DEBUGGED:true => id="DEBUGGED:true" (or id="debugged:true" if you use the convertToLowerCase parameter)
someAttribute=Any.Thing.Goes => someAttribute="Any.Thing.Goes"

这篇关于innerHTML 删除 Internet Explorer 中的属性引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 15:11