Home  >  Article  >  Web Front-end  >  What is the difference between where Js is placed in the HTML file_javascript skills

What is the difference between where Js is placed in the HTML file_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:24:531725browse

This question has always puzzled beginners. First understand where js can be placed in HTML, namely in the head and body. Most people put it in the head. When I was learning it, I confusedly followed it and put it in the head. I don’t know why? Today let’s talk about the difference between these two places:
First look at a piece of html code:

Copy the code The code is as follows:



New Document


, it enters the test.js file. The previous one will not run, that is, the one wrapped by function will not be run. At this time, the last sentence will be executed. Go to the page and get the element whose element ID is btn. But at this time, the HTML page has not been loaded. It is definitely not possible to get the element with id btn. An error will be reported. At this time, someone said that it can be changed to the following code:
Copy the code The code is as follows:

document .body.onload = function(){
document.getElementById("btn").onclick=test;
};

But writing it like this is not as good as writing .
Have you noticed that there are no parentheses in test above [document.getElementById("btn").onclick=test;], so what will happen if it is changed to [test()]?
What is the difference between where Js is placed in the HTML file_javascript skills
The result is as shown in the figure. The page is loaded like this, and there is no response when clicking the button. Change the js code to the following:
Copy the code The code is as follows:

var test=function( ){
var span = document.createElement("span");
span.innerHTML="add";
document.getElementById("target").appendChild(span);
return function (){
alert("aaaa");
};
}

document.getElementById("btn").onclick=test();

When the page is loaded, it still looks like the above. When the button is clicked, a box pops up with the content "aaaa"; indicating that the return value in the function is executed when the button is clicked. That is, when parentheses are added, the function will be executed even if the event is not triggered. The return value of the function executed when the event is triggered. When parentheses are not included, the function is executed only when the event is triggered.

Html event trigger, what content can be written?
•For example, onclick="";what can be written in double quotes. Generally speaking, you can write functions, for example, onclick="test();". What else can be written besides this? Can we omit this semicolon?
•Look at the js code above, there is a semicolon in every line. The purpose of semicolons is to obfuscate statements. That means you can write js code in onclick. Write a try, as follows
Copy the code The code is as follows:



New Document














•The running results are as follows:
What is the difference between where Js is placed in the HTML file_javascript skills
•The instructions are operational. This shows that you can put more than just function names.

Event binding method?
•There are two commonly used methods of event binding. One is adding js code to the event as introduced earlier. For example: onclick="test();". The disadvantage of this binding method is that you have to modify the code that the artist has already written.
• Another way is to use the ID as I started writing the code. The artist only needs to add the ID to each element. There is no need to modify the HTML code.
Statement:
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