Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

JavaScript is a common method of interaction


May 29, 2021 Article blog



JavaScript is powerful enough to do many new things, and here are some common ways to interact.

The output

document.write()

You can use it as content to output streams directly to HTML, in other words, to output content directly in a Web page.

First, the content that needs to be output should be surrounded by "" and the content in the "direct output" sign

<script type="text/javascript">
  document.write("I love JavaScript!"); //内容用""括起来,""里的内容直接输出。
</script>

Second, output content through variables

<script type="text/javascript">
  var mystr="hello world!";
  document.write(mystr);  //直接写变量名,输出变量存储的内容。
</script>

Third, output more than one content, the interval is connected with a sign.

<script type="text/javascript">
  var mystr="hello";
  document.write(mystr+"I love JavaScript"); //多项内容之间用+号连接
</script>

4. Output HTML tags and surround them with ""

<script type="text/javascript">
  var mystr="hello";
document.write(mystr+"<br>");//输出hello后,输出一个换行符
  document.write("JavaScript");
</script>

instance:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>document.write</title>
  <script type="text/javascript">
    var mystr="我是";
    var mychar="JavaScript";
    document.write(mychar+"<br>");
    document.write(mystr+mychar+"的忠实粉色!")
 
  </script>
</head>
<body>
</body>
</html>

That's all you need to know about JavaScript's common interaction methods.