[英]Why wont this javascript run?
有人為我創建了這個代碼: 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>"
代碼將不再運行。 我究竟做錯了什么?
您在語法錯誤:
newElement.innerHTML = "<a href="survey.php">Click Me To Continue</a>"
雙引號不能在雙引號字符串中使用。 轉義或更改為單引號和雙引號的組合:
newElement.innerHTML = '<a href="survey.php">Click Me To Continue</a>';
您在語法錯誤:
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/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.