如何显示javascript变量

如何显示javascript变量,javascript,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=devic

我在网站上查到一个问题

但是,当在我自己的编码程序中执行它时,它不起作用。希望你能帮忙

<!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>

因为您有语法错误。。检查控制台

正确的代码应为:

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

仅供参考:SyntaxError:预期表达式,获得感谢!对javascript比较陌生,所以我完全忘记了控制台。