javascript - Codecademy jQuery 卡在 "Modifying HTML Elements"

标签 javascript jquery

我们正在制定待办事项列表

以下是说明:

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/

相关文章:

javascript - 从所有行中获取单击的单选按钮值

javascript - 保持你的 javascript 结构化和整洁(作为一个 OO 程序员)

javascript - 使用 Shrinksafe 合并 JS 文件

javascript - 在javascript中展平嵌套对象

javascript - YouTube API - 在全局变量中获取当前时间

javascript - 需要将 JSON 对象插入 Angularjs Controller 范围

javascript - 将部分不同类名替换为数据属性

javascript - 我的 ajax/jQuery 函数不适用于动态加载的分页内容

JQuery 可拖动元素在放置时发生变化

javascript - 如何在单击按钮时克隆 div 换行?