1

有人为我创建了这段代码: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>"

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

4

2 回答 2

3

您在以下方面有语法错误:

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

双引号不能在这样的双引号字符串中使用。转义或更改为单引号和双引号的组合:

newElement.innerHTML = '<a href="survey.php">Click Me To Continue</a>';
于 2013-10-15T10:40:30.493 回答
0

您在以下方面有语法错误:

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

检查控制台,你会看到:

语法错误:缺少;before statement 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/

于 2013-10-15T10:42:11.793 回答