Javascript Append 不是函数 jquery
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38332179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Append is not a function jquery
提问by Everyone_Else
In the following code, I'd like to be able to append test_div
to the element that was clicked on. However, I get the error message that TypeError:
e.srcElement.appendis not a function
.
在下面的代码中,我希望能够附加test_div
到被点击的元素。但是,我收到了TypeError:
e.srcElement.append的错误消息is not a function
。
$(document).ready(function() {
onclick = function (e) {
var test_div = document.createElement("div");
target = e.srcElement
target.append(test_div);
}
$('#dialogue').on('click', onclick);
});
I've tried printing out the type of e.srcElement
, but it just says object
, which isn't very helpful. What's the correct way to append an element to the item clicked on?
我试过打印出 的类型e.srcElement
,但它只是说object
,这不是很有帮助。将元素附加到单击的项目的正确方法是什么?
回答by Milind Anantwar
.append()
is jquery method. You need to convert DOM object into jquery object for using jquery methods:
.append()
是jquery方法。您需要将 DOM 对象转换为 jquery 对象才能使用 jquery 方法:
$(target).append(test_div);
回答by binskits
Alternatively, the javascript method you can use is appendChild()
或者,您可以使用的 javascript 方法是 appendChild()
target.appendChild(test_div);
target.appendChild(test_div);
回答by Charlie
e.srcElement
is a DOM node. The correct method to append another element in to it is appendChild
e.srcElement
是一个 DOM 节点。将另一个元素附加到其中的正确方法是appendChild
.append
is the equivalent JQuery method.
.append
是等效的 JQuery 方法。
Hereis the documentation
这是文档
"I've tried printing out the type of e.srcElement
, but it just says object
"
“我试过打印出 的类型e.srcElement
,但它只是说object
”
console.log()
your debug messages.
console.log()
您的调试消息。
回答by Praveen Kumar Purushothaman
You need to use jQuery for this. Moreover, you are mixing vanilla JavaScript and jQuery. An alternate way of doing this is:
为此,您需要使用 jQuery。此外,您正在混合使用 vanilla JavaScript 和 jQuery。另一种方法是:
$(document).ready(function() {
$('#dialogue').on('click', function (e) {
var test_div = $("<div />");
$(this).append(test_div);
});
});