Home > Web Front-end > JS Tutorial > How to write JavaScript code in three different positions_javascript skills

How to write JavaScript code in three different positions_javascript skills

WBOY
Release: 2016-05-16 18:43:28
Original
1122 people have browsed it

The following is a list of three different places to write JavaScript code. The effect is to click the button button to pop up the alert warning box

The first one is the most common, the code is as follows
html code

js code
function pop()
{
alert("Called at JavaScript function");
}

The second is the simplest implementation method , the code is as follows


The third way is relatively complicated, the code is as follows
html code

js code
var obj=document.getElementById( "btn3");//The following statements must be placed below the definition of btn3, otherwise the compiler will not recognize btn3.
if(window.addEventListener)//Mozilla, Netscape, Firefox and other browsers
{
obj.addEventListener("click",fun,false);//Note the false here
}
else //IE browser
{
obj.attachEvent("onclick",fun);
}
function fun()
{
alert("By in function Trigger event in ");
}

Summary: The effect achieved by the three writing methods is exactly the same. It should be said that the three methods are commonly used, and each has its own advantages and disadvantages. . . .

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