我正在尝试创建一个“待办事项”列表,当用户输入新任务并单击按钮时,javascript应创建一个li元素,其中包含一个保存用户输入的跨度,然后将该li元素添加到ol中我的HTML。我的HTML看起来像这样:
<body>
<h1>To Do:</h1>
<section>
<input type="text" id="add_todo">
<span id="add_task_error"> </span>
<input type="button" id="add_task" value="Add task">
<div id="empty_message" class="open">
<h3>You have no tasks left to accomplish!</h3>
</div>
<div id="tasklist">
<ol class="list">
</ol>
</div>
</section>
</body>
这是不起作用的功能:
var newSpan = $('<span>input</span>').addClass("task");
//wrap it in a <li> element
newSpan = (".task").wrap("<li></li>");
$(".list").append(newSpan);
我也这样尝试过:
var new_task = $('<li>*</li>').addClass('task');
new_task.appendTo('ol.list');
new_task.setAttribute('id', 'new_task');
$("#new_task").text(input);
两种方法都不起作用-当我单击添加任务按钮时(这不是问题,我对其进行了测试),屏幕上没有任何反应...
我究竟做错了什么???
创建元素,设置所有属性,完成后将其添加到中ol
。
var new_task = $('<li></li>').addClass('task');
new_task.text($("#add_todo").val()); //this is the value of the input
new_task.attr('id', 'new_task'); //use attr instead of setAttribute
new_task.appendTo('ol.list');
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句