我是 jQuery 的初学者,我需要将以下 div 附加到 this.element
var div = '<div id="labels" style="background:' + this.getBottomLabelColorForPercentage() + '; color:white; width:' + this.element.clientWidth + '; text-align:center; height:30px;"><div style="padding-top: 1px;">' + this.getNeedlePointerValue() + this.model.properties.rightlabel + '</div></div>';
div.appendTo(this.element);
但是抛出异常
sourcefile.js:71 Uncaught TypeError: div.appendTo is not a function
如何将我的 div 有效地添加到 this.element
中?
最佳答案
在您的情况下 div
是一个字符串变量,因此它不包含任何 DOM 或 jQuery 相关函数。您需要首先将该字符串转换为 jQuery 元素。
创建您需要使用的 jQuery 对象
$()
函数带有 htmlString
参数:
$(div).appendTo(this.element);
关于javascript - 如何将一个div附加到另一个div中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47013276/