发现打开/关闭后台脚本的问题
我有两个div
<div class="parallax__layer parallax__layer--back" id="par_back">
<div id="back">
<canvas></canvas>
<canvas></canvas>
<script src="js/index.js"></script>
</div>
</div>
和将删除此代码以使用并重新粘贴它的代码的函数
function Eco() {
var checkBox = document.getElementById("eco");
var script = document.getElementById("back");
// If the checkbox is checked, display the output text
if (checkBox.checked == true){
script.parentNode.removeChild(script);
} else {
script.innerHTML = "<div id="back"><canvas></canvas><canvas></canvas><script src="js/index.js"></script></div>";
}
}
但innerHTML不适用于div(
答案 0 :(得分:1)
看起来script.innerHTML行有问题。您需要转义字符串中的双引号,或使用单引号括起来。尝试其中之一:
逃逸:
script.innerHTML = "<div id=\"back\"><canvas></canvas><canvas></canvas><script src=\"js/index.js\"></script></div>";
单引号:
script.innerHTML = '<div id="back"><canvas></canvas><canvas></canvas><script src="js/index.js"></script></div>';