简体   繁体   English

尝试创建新元素时出错

[英]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.

相关问题 尝试使用 JavaScript 打开新窗口时出错 - Error while trying to open a new window with JavaScript 尝试创建新的 React Native 项目时出错 - Error trying to create new react native project 尝试从数组访问元素时Javascript错误 - Error in Javascript while trying to access element from array 尝试放置新项目时出现 DynamoDB InvalidParameterType 错误 - DynamoDB InvalidParameterType Error While Trying To Put A New Item 尝试将应用程序重定向到新服务器端口时出错 - Error while trying to re-direct application to new server port Photoshop Javascript-尝试创建新的文本层时出现语法错误 - Photoshop Javascript - syntax error when trying to create new text layer 尝试在 create-react-app 中导入 npm 时出错 - Error while trying to import npm got in create-react-app 尝试在ember.js中创建一对一关系时出错 - Error while trying to create a one to one relationship in ember.js “失败:尝试创建此媒体项目时出错。” - “Failed: There was an error while trying to create this media item.” 尝试在Firebase FireStore中创建函数时Javascript Promises错误 - Error in Javascript Promises while trying to create a function in Firebase FireStore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM