简体   繁体   English

这个我的javascript代码怎么了? 它不适用于IE8

[英]Whats wrong with this my javascript code ? it is not working in IE8

<html>
<head></head>
<body>
<div class="abcd"></div>
<style>
.new_info {
color:red;
}
</style>
<script>

function getBox(){
    var sende = document.querySelectorAll(".abcd");

    alert(sende[0].hasChildNodes());
    alert(y[0].hasChildNodes());
    if(sende[0].hasChildNodes() == false && sende[0].nodeValue == null){
            var newdiv = document.createElement('div');
            newdiv.innerHTML = "which i want";
            newdiv.className = "new_info";
            sende[0].appendChild(newdiv);
}
}
getBox();

</script>
</body>
</html>

In above code i want to fill div tag having class 'abcd' with some div as childnode having some text like 'which i want'. 在上面的代码中,我想填充具有类'abcd'的div标签,其中一些div作为具有一些文本的子节点,如'我想要'。 this work well in other browser but not in IE8. 这在其他浏览器中工作得很好,但在IE8中却没有。 what should i do to work this code in IE8 too ? 我该怎么做才能在IE8中使用这段代码呢?

The querySelectorAll does not work in IE8 eg only works only in IE8 standards mode and there are bug reports of it when it comes to IE8. querySelectorAll在IE8中不起作用,例如仅适用于IE8标准模式 ,并且在涉及IE8时存在错误报告

For solution check out: 对于解决方案检查:

Sadly or luckily because of these cross browser issues, there exist JavaScript libraries to benefit from which clear all that mess for us internally. 遗憾或幸运的是,由于这些跨浏览器问题,存在JavaScript库以从中受益,从而清除了内部的所有混乱。

What this code is doing alert(y[0].hasChildNodes()); 这段代码正在做什么alert(y[0].hasChildNodes()); in your code, because of this it is not executing. 在你的代码中,因为它没有执行。

See Demo: http://jsfiddle.net/rathoreahsan/NmFNH 见演示: http//jsfiddle.net/rathoreahsan/NmFNH

i have commented alert(y[0].hasChildNodes()); 我已经评论过alert(y[0].hasChildNodes()); and it is working. 它正在发挥作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM