javascript
  • html
  • 2017-08-15 68 views 1 likes 
    1
    document.body.innerHTML = "An error occurred. Please try to login again.<br>"; 
    
    document.body.innerHTML += 'Username: <input type="text" name="username"><br>'; 
    document.body.innerHTML += 'Password: <input type="password" name="password"><br>'; 
    document.body.innerHTML += "<input type='submit' value='Login' onclick='alert(document.getElementById('password').value)'>" 
    

    運行該代碼,然後點擊登錄按鈕後,Chrome的JavaScript控制檯還給:注入腳本DOM

    SyntaxError: expected expression, got '}'

    我怎樣才能解決呢?

    回答

    3

    您需要更改報價,並逃離他們的JavaScript代碼中:

    document.body.innerHTML = "An error occurred. Please try to login again.<br>"; 
     
    
     
    document.body.innerHTML += 'Username: <input type="text" name="username"><br>'; 
     
    document.body.innerHTML += 'Password: <input type="password" name="password" id="password"><br>'; 
     
    document.body.innerHTML += "<input type='submit' value='Login' onclick='alert(document.getElementById(\"password\").value)'>";

    As for the reason "why the browser throws this specific error?", here is the question:

    因爲代碼是錯誤的,瀏覽器實際看到下面的代碼:

    (function(event){alert(document.getElementById(
    }) 
    

    正如你所看到的 - 在document.getElementById(有一個關閉{,這是您的瀏覽器正在向您大喊大叫。

    +0

    點擊登錄按鈕。 – WayneXMayersX

    +0

    @SpencerWieczorek實際上有一個錯誤:),我更新了答案 – Dekel

    +0

    @WayneXMayersX現在檢查答案 – Dekel

    1

    只是逃避報價

    document.body.innerHTML = "An error occurred. Please try to login again.<br>"; 
     
    
     
    document.body.innerHTML += 'Username: <input type="text" name="username"><br>'; 
     
    document.body.innerHTML += 'Password: <input type="password" name="password" id="password"><br>'; 
     
    document.body.innerHTML += "<input type='submit' value='Login' onclick='alert(document.getElementById(\"password\").value);return false;' />"

    相關問題