3

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>

次のエラーが発生し続けます。

終了していない文字列リテラル であり、エラーが最初の P タグにあることを示しています

innerHTML 関数に渡す HTML に対して何らかの処理を行う必要がありますか?

前もって感謝します!

4

3 に答える 3

5

Unterminated string literal通常、次のようなことをしようとしたことを意味します。

var str = "Blah blah blah
  more blah that should be part of the string
  even more blah.";

JavaScript でこれを行うことはできません。文字列を終了して次の行を追加する必要があります。

var str = "Blah blah blah\n"
  +"more blah that should be part of the string\n"
  +"even more blah.";

または、改行をエスケープすることもできます (これが完全に標準かどうかはわかりません)。

var str = "Blah blah blah\
  more blah that should be part of the string\
  even more blah.";

また、変更しようとしている要素がまだ定義されていないことにも注意してください。<div>変更しようとしている が の前にあることを確認するか<script>、スクリプトまたはその内容 (window.onloadまたは同様のもの)を延期します。

于 2012-09-17T15:04:25.213 に答える
0

テキストに改行が含まれているようです。

機会があれば、割り当てる前に休憩のためにそれらを交換するかもしれません

stringWithReturnsIn.replace(/[\n\r]/g, '<br />');

それ以外の場合は返品を保持する場合は br

stringWithReturnsIn.replace(/[\n\r]/g, ' ');
于 2012-09-17T15:04:44.540 に答える
0

問題は、使用している派手な引用符 (' および ') である可能性があります。' に置き換えてみてください。

このように HTML をページに挿入することは、解析エラーが多く、XSS の脆弱性が生じる可能性があるため、一般的には適切な方法ではありません。document.createElement()子要素にテキスト コンテンツを追加して内部要素を作成することを検討してください。もう少し冗長ですが、構造が同じままであると仮定すると、はるかにシームレスに変更できます。

于 2012-09-17T15:07:57.293 に答える