Javascript jQuery左侧错误无效
我收到此线路的控制台错误:Javascript jQuery左侧错误无效,javascript,jquery,Javascript,Jquery,我收到此线路的控制台错误: $("#note" + noteCount).val() = note; 以下是完整的功能: function addNotes() { noteCount++; var note = $("#noteInput").val(); console.log("Note: " + note); console.log("Note Count: " + noteCount); var display = document.creat
$("#note" + noteCount).val() = note;
以下是完整的功能:
function addNotes() {
noteCount++;
var note = $("#noteInput").val();
console.log("Note: " + note);
console.log("Note Count: " + noteCount);
var display = document.createElement("div");
document.getElementById("displayContainer").appendChild(display);
display.className = "noteDisplay";
display.id = "note" + noteCount;
$("#note" + noteCount).val() = note;
}
正如您所看到的,我正在创建一个id为“note”+note count的div,但我不确定如何选择它来更改其值。请尝试
$("#note" + noteCount).val( note );
调用返回一个不能用其他值覆盖的值
val = note; //has a different meaning, still not correct for your scenario
但是
由于值已通过函数调用计算,因此左侧不是变量,而是值,并且您无法将值分配给值,所以请尝试
$("#note" + noteCount).val( note );
调用返回一个不能用其他值覆盖的值
val = note; //has a different meaning, still not correct for your scenario
但是
由于值已通过函数调用计算,因此左侧不是变量,而是一个值,并且您无法将值分配给值,因此您分配值的方式是错误的。您需要将其作为参数传递给
val
函数,因为它是函数而不是属性
$("#note" + noteCount).val(note) ;
请参见指定值的方式错误。您需要将其作为参数传递给
val
函数,因为它是函数而不是属性
$("#note" + noteCount).val(note) ;
请参阅使用
jQuery
时,应将值放入()
的
中
$("#note" + noteCount).val(note);
使用
jQuery
时,应将值放入val
的()
中
$("#note" + noteCount).val(note);
在创建
DIV时,最好使用textContent
属性
var display = document.createElement("div");
display.id = "note" + noteCount;
display.textContent = note;
或者,由于DIV
没有value
属性,您需要将其作为参数提供给方法调用:
$("#note" + noteCount).text(note);
在使用jQuery时,使用它创建HTML
var display = $("<div>", {
"text" : note,
"id" : "note" + noteCount,
"class" : "noteDisplay"
});
display.appendTo('#displayContainer');
var display=$(“”{
“文本”:注,
“id”:“注释”+注释计数,
“类”:“注释显示”
});
display.appendTo(“#displayContainer”);
在创建DIV时,最好使用textContent
属性
var display = document.createElement("div");
display.id = "note" + noteCount;
display.textContent = note;
或者,由于DIV
没有value
属性,您需要将其作为参数提供给方法调用:
$("#note" + noteCount).text(note);
在使用jQuery时,使用它创建HTML
var display = $("<div>", {
"text" : note,
"id" : "note" + noteCount,
"class" : "noteDisplay"
});
display.appendTo('#displayContainer');
var display=$(“”{
“文本”:注,
“id”:“注释”+注释计数,
“类”:“注释显示”
});
display.appendTo(“#displayContainer”);
我以前在查看文档时感到困惑,因为它说“此方法不接受任何参数”。谢谢:)没问题:)。很高兴为您提供帮助我马上就来:)我相信您是对的,但当我测试它时,它不会显示出来。正在使用id和类名创建div,但该类没有生效。我在查看文档时感到困惑,因为它说“此方法不接受任何参数”。谢谢:)没问题:)。很高兴为您提供帮助我马上就来:)我相信您是对的,但当我测试它时,它不会显示出来。正在使用id和类名创建div,但是该类没有生效,因为它是div
元素。我将扩展答案以改进逻辑,从而消除createElement
/appendChild
的丑陋。很好地发现它是div
元素。我将扩展答案以改进逻辑,从而消除createElement
/appendChild
的丑陋。可能的重复