【发布时间】:2022-10-07 08:29:26
【问题描述】:
我知道这被问了很多,我已经尝试了一些来自 SO 的例子,但没有运气。
我有一个功能可以让我更改 div 中的文本并且它可以工作,但只能作为纯文本。我希望它作为 HTML 工作,但我尝试将其放入 innerHTML,可能是在错误的位置,并在我的脚本中用 + 符号分隔 HTML。似乎没有任何效果。要么我得到原始文本中的 HTML,要么该函数根本不起作用。
这是我此时的脚本:
$(function() {
var copyStack = [
\'<strong>This is bold text within strong tags,</strong>\' + \' this is the remainder of my copy.\',
\'<strong>This is more bold text within strong tags,</strong>\' + \' this is the remainder of my second copy.\'
];
$(\"#swatch-0\").click(function () {
$(\".product-shop-description\").text(copyStack[0]);
console.log(copyStack[0]);
});
$(\"#swatch-1\").click(function () {
$(\".product-shop-description\").text(copyStack[1]);
console.log(copyStack[1]);
});
});
-
text设置纯文本,而不是 HTML,请改用html方法。
标签: javascript