2016-05-27 70 views
-2

我已經看過了相關網站(How to display javascript variables in a html page without document.write如何顯示JavaScript的變量

上,但在我自己的編碼程序執行時,它不工作。希望你能幫忙!

<!DOCTYPE html> 
<html> 
<head> 
    <script src="https://code.jquery.com/jquery-1.11.3.js"></script> 
    <meta charset ="UTF-8"> 
    <meta name="viewport" content="width=device-width"> 

    <style type="text/css"> 
    html { 
    font-family: sans-serif; 
    font-size: 16px; 
    } 

    h1 { 
    margin: 1em 0 0.25em 0; 
    } 

    input[type=text] { 
    padding: 0.5em; 
    } 

    .jsValue, .jqValue { 
    color: red; 
    } 
    </style> 



</head> 
<body> 
    <!-- This <input> field is where I'm getting the name from --> 
    <label>Enter your name: <input class="name" type="text" value="World"/></label> 

    <!-- Plain Javascript Example --> 
    <h1>Plain Javascript Example</h1>Hello <span class="jsValue">World</span> 

    <!-- jQuery Example --> 
    <h1>jQuery Example</h1>Hello <span class="jqValue">World</span> 

    <script> 
    //https://stackoverflow.com/questions/9689109/how-to-display-javascript-variables-in-a-html-page-without-document-write 
    // Plain Javascript Example 
    var $jsName = document.querySelector('.name'); 
    var $jsValue = document.querySelector('.jsValue'); 

    $jsName.addEventListener('input', function(event)){ 
    $jsValue.innerHTML = $jsName.value; 
    }, false); 

    </script> 

</body> 
</html> 

回答

0

因爲你有語法錯誤..檢查控制檯。

正確的代碼應該是:

$jsName.addEventListener('input', function(event){ 
    $jsValue.innerHTML = $jsName.value; 
    }, false); 
+1

參考:'語法錯誤:預期的表現,得到了 ')'' –

+0

謝謝!新的JavaScript,所以我完全忘了控制檯。 – Bodhidharma482