使用innerHTML添加有序列表在IE中失败

发布于 2024-09-04 18:33:31 字数 944 浏览 2 评论 0原文

我使用以下 Javascript 代码来填充带有有序列表的 DIV:

// send script back in split list
var scriptList = script.split("\n");
var finalScript = "<ol>\n";
var count = 0;
while(scriptList.length >= count) {
    if((scriptList[count]=="") || (scriptList[count] == undefined))  {
        count ++;
        continue;
    }

    finalScript = finalScript + "<li>" + scriptList[count] + "</li>\n";
    count ++;
}
finalScript = finalScript + "</ol>";
scriptingDiv.innerHTML = finalScript;

在 Firefox 中,如果我使用 Firebug 查看 DOM,这将正确转换为以下内容并正确显示有序列表。

<ol>
<li>This is the first item in the list</li>
<li>This is the second item in the list</li>
</ol>

在 IE 中,它显示为 标签是
>标签并忽略所有其他标签,如下所示:

这是列表中的第一项
这是列表中的第二项

我是否需要将有序列表动态添加到 DOM 才能正常工作?与仅使用 .innerHTML 在 div 中设置 html 代码相反?

TIA

I'm using the following Javascript code to populate a DIV with an ordered list:

// send script back in split list
var scriptList = script.split("\n");
var finalScript = "<ol>\n";
var count = 0;
while(scriptList.length >= count) {
    if((scriptList[count]=="") || (scriptList[count] == undefined))  {
        count ++;
        continue;
    }

    finalScript = finalScript + "<li>" + scriptList[count] + "</li>\n";
    count ++;
}
finalScript = finalScript + "</ol>";
scriptingDiv.innerHTML = finalScript;

In firefox, if i look in the DOM using Firebug, this correctly translates to the following and correctly displays an ordered list.

<ol>
<li>This is the first item in the list</li>
<li>This is the second item in the list</li>
</ol>

In IE, it displays as if the </li> tags are <br /> tags and ignores all the other tags, like this:

This is the first item in the list
This is the second item in the list

Do I need to dynamically add the ordered list to the DOM for this to work? As opposed to just setting the html code in the div using .innerHTML?

TIA

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟花肆意 2024-09-11 18:33:31

我是否需要将有序列表动态添加到 DOM 才能正常工作?与仅使用 .innerHTML 在 div 中设置 html 代码相反?

是的

var scriptList = script.split("\n");
var count = 0;
var ol = document.createElement("ol");

    for(var index=0; index<scriptList.length; index++) {
        if(scriptList[index]!="")  {
            var li = document.createElement("li");
            li.innerHTML=scriptList[index];
            ol.appendChild(li);
        }
    }

scriptingDiv.appendChild(ol);

Do I need to dynamically add the ordered list to the DOM for this to work? As opposed to just setting the html code in the div using .innerHTML?

Yes

var scriptList = script.split("\n");
var count = 0;
var ol = document.createElement("ol");

    for(var index=0; index<scriptList.length; index++) {
        if(scriptList[index]!="")  {
            var li = document.createElement("li");
            li.innerHTML=scriptList[index];
            ol.appendChild(li);
        }
    }

scriptingDiv.appendChild(ol);
就此别过 2024-09-11 18:33:31

为什么不使用 dom 方法来代替呢?即:

myOL = document.createElement("ol");
myLI = document.createElement("li");
myTxt = document.createTextNode("My Text!");
myLI.appendChild(myTxt);
myOL.appendChild(myLI);

Why not use dom methods instead? IE:

myOL = document.createElement("ol");
myLI = document.createElement("li");
myTxt = document.createTextNode("My Text!");
myLI.appendChild(myTxt);
myOL.appendChild(myLI);

etc

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文