我们正在制定待办事项列表
以下是说明:
Perfect! Now we want to add our HTML element to the document. We can do this using our handy .append() function. Let's go ahead and append to our div with the .list class. We'll append a with class="item", since we'll want a way to target our appended s later when we remove them. (A "to do" list is no good if we can't check things off it.) We'll want the contents of our div to be the contents of our input field, which we saved in the variable toAdd. That means when we append, we'll want to append:
'<div class="item">' + toAdd + '</div>'
Go ahead and .append() a with class="item" to the .list div of your HTML document, then MAKE SURE to click your button to add an item—the exercise will wait for you to do so!
我不明白需要做什么。
我尝试过这个:
$(document).ready(function() {
$("button").click(function () {
var toAdd = $("input[name=checkListItem]").val();
$(".list").append("div class='item'" + toAdd + "div");
});});
但是当我输入任何内容并单击按钮时它不起作用。
最佳答案
关闭!试试这个:
$(document).ready(function() {
$("#button").click(function() {
var toAdd = $("input[name=checkListItem]").val();
$(".list").append("<div class='item'>" + toAdd + "</div>");
});
});
您的append() 中没有HTML 尖括号。另外,$("button")
应该是$("#button")
因为它是 ID“按钮”,而不是真实的 <button>
.
关于javascript - Codecademy jQuery 卡在 "Modifying HTML Elements",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29736936/