Click a hyperlink to contact the event
1. Use the onclick with a tag
<a href="#" onclick="js code">
There is a disadvantage in this way of writing, which is that the page will be refreshed after clicking and returned to the top of the page.
How to do it without refreshing?
The code copy is as follows:
<a href="javascript:void(0)" onclick="js code">
2. The difference between # and javascript: void (0)
When the hyperlink is "dead link", the difference between using # and javascript:void(0);
Void (0) has no use value, it won't go anywhere, so it's meaningless
And # will refresh the current page once.
3. Or, use the href tag of a
<a href="javascript: js code">
Only JavaScript in the hyperlinked href needs to be added "javascript:".
Because it is not an event, it is regarded as a network protocol like "javascript:", "ftp:", "thunder://", "ed2k://", and "mailto:", and handed over to the js parsing engine for processing.
Special emphasis on method
1. Don't start another line
Generally speaking, when writing curly braces in js, you will follow them directly without starting a new line.
The reason is that js will automatically add "semi-colon" at the end of the statement. If curly braces are put together, then after automatically adding "semi-colon" after the return statement, the return value becomes undefined.
2. No need to declare the return value type
There is no need to declare the return value type or parameter type. Function definitions begin with a function.
In JavaScript, it is not like C# and java that requires all paths to have return values but no return values is undefined.
3. No method overloading
There is no method overload in JavaScript.
Write parameters in the method called named parameters
When a user calls a method to calculate the sum of multiple numbers, how to get how many parameters are there in the method?
Please use the arguments object!
Anonymous functions
1. Method one
The code copy is as follows:
var f1=function(p1,p2){ return p1+p2; };// Assign the function to a variable
Anonymous functions cannot be called, they can only be assigned to a variable. Since it is an assignment statement, a point-colon must be added later.
application:
The code copy is as follows:
document.getElementById('btn').onclick=function(){}
2. Method 2
The code copy is as follows:
(function(p1,p2){alert(p1+p2);})(20,30);
3. Method Three
The code copy is as follows:
var m1=new Function("p1","p2","p3","return p1+p2+p3");
alert(m1(1,2,3)); low performance
There are many usages of this anonymous function in jQuery
Extend method prototype
Set the extension method through the prototype of the class object,
The declaration of the extension method is executed before using the extension method. JS functions do not have a special function default value syntax, but you can not pass values to parameters. The parameter values that do not pass values are undefined. Make your own judgment to give the default value.
The code copy is as follows:
var msg ='[email protected]';
String.prototype.isEmail = function(){
return this.indexof('@')!= -1 ?true:false;
};
alert(msg.isEmail);
event
1. Call events
2. Register events
Events can only be registered for elements of the page after the page is loaded.
So onload = function(){
//Register events here
};
Page loading event
The code copy is as follows:
<script type="text/javascript">
//refresh
//location.reload();
onload=function(){
alert("page loaded");
};
onunload = function(){
alert("Page closed");
};
onbeforeunload= function(){
alert("Flashed before page closes");
};
</script>
The above is all about this article. I hope it can give you a new understanding of JavaScript events and methods.