[英]Error while trying to create new element
I use the following code and I got error when I do appendChild 我使用以下代码,当我执行appendChild时出现错误
$('#tra').on('click', function() {
transpile(source.val())
// ...
var display = $('#display');
diff.forEach(function(part) {
var span = document.createElement('span');
span.style.color = color;
span.appendChild(document
.createTextNode(part.value));
display.appendChild(span);
In the last code I got error: 在上一个代码中,我得到了错误:
main.js:42 Uncaught TypeError: display.appendChild is not a function ,what it can be? main.js:42未捕获的TypeError:display.appendChild不是函数,可以是什么?
in the index.html I've the display as follows 在index.html中,我的显示如下
Your display
variable is a jQuery object. 您的display
变量是一个jQuery对象。 It has a .append()
method that should be fine for what you need. 它有一个.append()
方法, .append()
您的需求。
display.append(span);
If you want to use the JavaScript DOM function .appendChild()
instead, you need to extract the DOM element from display first: 如果要改用JavaScript DOM函数.appendChild()
,则需要先从显示中提取DOM元素:
display.get(0).appendChild(span);
First, check if your #display
element exists in your document (DOM). 首先,检查您的#display
元素是否存在于文档(DOM)中。
Second, there is no appendChild
method in jQuery, so you should use: 其次,jQuery中没有appendChild
方法,因此您应该使用:
display.append(span)
Or 要么
$(span).appendTo(display);
Otherwise, if you want to use appendChild
anyway, you'd use: 否则,如果您仍然想使用appendChild
,请使用:
display[0].appendChild(span);
Or by using $.get method: 或使用$ .get方法:
display.get(0).appendChild(span);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.