简体   繁体   English

getElementsByTagName IE问题

[英]getElementsByTagName IE Issue

This code works well in all major browsers except Internet Explorer (Opera, Firefox, Crome..) 此代码适用于除Internet Explorer(Opera,Firefox,Crome ...)之外的所有主流浏览器

Every second js issue is related to IE. 每秒js问题都与IE有关。 Thanks in advance. 提前致谢。

<script type="text/javascript">
function myFunction(nesto,param)
{
var str = nesto;
    if (param == "latin")
    {
    str = str.replace(/Џ/g,"DŽ");
    str = str.replace(/Љ/g,"LJ");
    }
    if (param == "cirilic")
    {
    str = str.replace(/DŽ/g,"Џ");
    str = str.replace(/LJ/g,"Љ");
    }
return str;
}
function konvertor(param) {
 for (i=0;i<document.getElementsByName("jezik").length; i++) {
document.getElementsByName("jezik").item(i).innerHTML = myFunction(document.getElementsByName("jezik").item(i).innerHTML,param);
 }
}
</script>
<button onclick="konvertor('latin')">latinica</button>
<button onclick="konvertor('cirilic')">cirilica</button>
<div name="jezik">DŽ LJ</div>
<div name="jezik">DŽ LJ</div>
function konvertor(param) {
    var len=document.getElementsByName("jezik").length;
    for (i=0;i<len; i++) {
        // use array access instead of item()
        document.getElementsByName("jezik")[i].innerHTML = myFunction(document.getElementsByName("jezik")[i].innerHTML,param);
    }
}

getElementsByName fetches only elements by their name, when they have a name-attribute(following the specification/DTD). Those are eg form-elements and images, but not <div> 当getElementsByName have a name-attribute(following the specification/DTD). Those are eg form-elements and images, but not <div>时,它们只按名称获取元素have a name-attribute(following the specification/DTD). Those are eg form-elements and images, but not <div> have a name-attribute(following the specification/DTD). Those are eg form-elements and images, but not <div> -elements. have a name-attribute(following the specification/DTD). Those are eg form-elements and images, but not <div>元素。

You may use querySelectorAll() instead: 您可以使用querySelectorAll()代替:

document.querySelectorAll("*[name='jezik']")

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

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