创建li并追加到ol的Javascript [英] Javascript to create an li and append to an ol

查看:70
本文介绍了创建li并追加到ol的Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JavaScript创建li并将其附加到现有的ol.我正在使用的代码是

I'm trying to use JavaScript to create an li and append it to an existing ol. The code I am using is

<ol id=summaryOL>
</ol>

function change(txt) {
var x=document.getElementById("summaryOL");
newLI = document.createElementNS(null,"li");
newText = document.createTextNode(txt);
newLI.appendChild(newText);
x.appendChild(newLI);
}

change("this is the first change");
change("this is the second change");

这些应该看起来像:

1. this is ...
2. this is ...

但是看起来像:

this is the first changethis is the second change

我在以下位置创建了一个小提琴:小提琴.感谢您的帮助.

I have created a fiddle at: fiddle . Thanks for any help.

推荐答案

这里是一个示例-jsfiddle

Here is an example - jsfiddle

$(function() {
    var change = function( txt ) {
        $("#summaryOL").append( '<li>' + txt + '</li>' );
    };

    change("this is the first change");
    change("this is the second change");
});

要使用jQuery ,请将以下代码放在< head></head>

To use jquery, put below code inside of <head></head>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

这篇关于创建li并追加到ol的Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆