更改段落时出现Javascript错误

时间:2014-12-08 20:02:14

标签: javascript

所以我试图让页面显示按下每个按钮的不同段落。但是,我不断收到一些错误,其中按钮将无法正常运行该功能。 实施例

function examplefunction() {
  document.getElemtentById("paragraphtobechanged").innerHTML = "When I put more "" it fails in other words it will not write this part."
  }
<button onclick="examplefunction()">Change Paragraph</button>
<p id="paragraphtobechanged"></p>

3 个答案:

答案 0 :(得分:2)

应该是:

function examplefunction() {
  document.getElemtentById("paragraphtobechanged").innerHTML = "When I put more \"\" it fails in other words it will not write this part."
  }

您需要转义双引号。或者你可以这样做:

function examplefunction() {
      document.getElemtentById("paragraphtobechanged").innerHTML = 'When I put more "" it fails in other words it will not write this part.'
      }

答案 1 :(得分:1)

@TheDude走在正确的轨道上,但还有别的东西。您在示例中拼错了元素:

function examplefunction() {
                V
  document.getElementById("paragraphtobechanged").innerHTML = "When I put more \"\" it fails in other words it will not write this part."
  }

答案 2 :(得分:0)

这里的问题是实际的字符串。

// Option 1
"When I put more \"\" it fails in other words it will not write this part."


// Option 2
'When I put more "" it fails in other words it will not write this part.'

注意:选项2适用于您的情况,但如果您在实际字符串中使用两个单引号和双引号,则必须转义其中一个。