简体   繁体   English

如何显示javascript变量

[英]How to display javascript variables

I have looked up a question on the website ( How to display javascript variables in a html page without document.write )我在网站上查了一个问题( How to display javascript variables in a html page without document.write

But when executing it in my own coding program it does not work.但是在我自己的编码程序中执行它时它不起作用。 Hope you can help out!希望你能帮忙!

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

Because you have syntax error.. check the console.因为你有语法错误..检查控制台。

Correct code should be:正确的代码应该是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM