为什么这个javascript不会运行?

时间:2013-10-15 10:36:51

标签: javascript

有人为我创建了这段代码: http://jsfiddle.net/kzFWa/

var downloadButton = document.getElementById("continue");
var counter = 60;
var newElement = document.createElement("p");
newElement.innerHTML = "<h3>or click here to continue in 60 seconds</h3>";
var id;

downloadButton.parentNode.replaceChild(newElement, downloadButton);

id = setInterval(function() {
    counter--;
    if(counter < 0) {
        newElement.parentNode.replaceChild(downloadButton, newElement);
        //newElement.innerHTML = "<a href="survey.php">Click Me To Continue</a>"
        clearInterval(id);
    } else {
        newElement.innerHTML = "<h3>or click here to continue in  " + counter.toString() + " seconds.</h3>";
    }
}, 1000);

但是,如果我改变

newElement.parentNode.replaceChild(downloadButton, newElement); 

newElement.innerHTML = "<a href="survey.php">Click Me To Continue</a>"

代码将不再运行。我做错了什么?

2 个答案:

答案 0 :(得分:3)

您的语法错误:

newElement.innerHTML = "<a href="survey.php">Click Me To Continue</a>"

双引号不能在双引号字符串中使用。要么转义要么改为单引号和双引号的组合:

newElement.innerHTML = '<a href="survey.php">Click Me To Continue</a>';

答案 1 :(得分:0)

您的语法错误:

newElement.innerHTML = "<a href="survey.php">Click Me To Continue</a>"

检查控制台,你会看到:

  

SyntaxError:missing;在声明之前newElement.innerHTML =“点击我继续”

因此代码编写得不好,请将其更改为:

newElement.innerHTML = "<a href='survey.php'>Click Me To Continue</a>";

参考:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Values,_variables,_and_literals

演示:http://jsfiddle.net/aY5PK/