Home > Web Front-end > Front-end Q&A > How to output value in javascript

How to output value in javascript

青灯夜游
Release: 2023-01-07 11:47:42
Original
3283 people have browsed it

Methods for outputting values: 1. Use the "window.alert('value')" statement to output through the pop-up window; 2. Use the "element object.innerHtml="output value" statement; 2. Use the "document .write("value")" statement; 4. Use the "console.log("value")" statement.

How to output value in javascript

The operating environment of this tutorial: windows7 system , javascript version 1.8.5, Dell G3 computer.

JavaScript output method

1. Use window .alert()Pop up warning box

<script>
window.alert(&#39;警告框&#39;);
</script>
Copy after login

2. Use element object .innerHtml="value to be written"Write to HTML element

If you need JavaScript to access an HTML element, you can use the document.getElementById(id) method

Please use the id attribute to identify the HTML element and innerHTml to get or insert the element Content:

<p id="demo" >我的第一个段落</p>
<script>
document.getElementById("demo").innerHTML="段落已修改"
</scirpt>
Copy after login

  • document.getElementById("demo") is the JavaScript code that uses the id attribute to find the HTML element. This method is defined in the HTML DOM.

  • innerHTML = "Paragraph changed." is the JavaScript code used to modify the HTML content (innerHTML) of the element.

3. Use the document.write() method to write the content into the html document

Use the document.write() method to write the content into the html document In HTML content, this function can directly write the content or html tags into the html document


<script>
document.write(Date());//Date()输出中国标准时间
</script>
Copy after login

How to output value in javascript

Note: If document.write is executed after the document has completed loading () will cover the entire HTML page


Sample:
<button type="button" onclick="myFunction">按钮</buton>
<script>
function myFunction(){
document.write(Date());
}
</scipt>
Copy after login

4. Use console.log() to write to the console of the server

If your browser supports it For debugging, you can use the console.log() method to display JavaScript values ​​in the browser.

Use F12 in the browser to enable debugging mode, and click the "Console" menu in the debugging window.

<script>
a = 5; 
b = 6; 
c = a + b; 
console.log(c); 
</scipt>
Copy after login

How to output value in javascript

[Recommended learning:

javascript advanced tutorial]

The above is the detailed content of How to output value in javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template