我在使用javascript更新div时遇到问题.
<script>
document.getElementById('advertisingBrandValues').innerHTML = "<p>The Mac is the most powerful creative tool in the world, giving users unbridled potential to make everything from bespoke family albums to industry-standard pop songs – iCreate is the key to unlocking that potential. Accessible, smart and authoritative, the iCreate brand thrives on the mantra ‘instruct, inform, inspire’, helping thousands of Mac users realise their creative ambitions.</p>";
</script>
<div id="advertisingBrandValues"></div>
Run Code Online (Sandbox Code Playgroud)
我一直收到以下错误:
未终止的字符串文字 ,它表示错误是第一个P标记
我是否需要做一些我传递给innerHTML函数的HTML?
提前致谢!
Unterminated string literal 通常意味着你试图做这样的事情:
var str = "Blah blah blah
more blah that should be part of the string
even more blah.";
Run Code Online (Sandbox Code Playgroud)
您不能在JavaScript中执行此操作,您必须结束字符串并附加下一行:
var str = "Blah blah blah\n"
+"more blah that should be part of the string\n"
+"even more blah.";
Run Code Online (Sandbox Code Playgroud)
或者你可以逃避换行(不确定这是否完全是标准的):
var str = "Blah blah blah\
more blah that should be part of the string\
even more blah.";
Run Code Online (Sandbox Code Playgroud)
另请注意,您尝试修改的元素尚未定义.要么确保<div>您要修改<script>或推迟脚本或其内容(window.onload或类似)