Home > Web Front-end > JS Tutorial > body text

Two ways to assign values ​​and pass parameters to onclick using js_javascript skills

WBOY
Release: 2016-05-16 17:12:34
Original
1189 people have browsed it

1) When the method has no parameters, the value can be assigned directly using onclick = method name

Copy code The code is as follows:

window.onload = function() {
$('btnTest').onclick = test;
}
function test() {
alert(val);
}

2) When the method has parameters, it is wrong to use onclick = method name (parameter). You need to add function() in front of the method name
Copy code The code is as follows:

window.onload = function() {
$('btnTest').onclick = function( ) { test(1) };
}
function test(val) {
alert(val);
}
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