javascript - The difference between writing ask() directly and calling it when clicking on onclick event
世界只因有你
世界只因有你 2017-05-19 10:31:06
0
2
398
window.onload=init;
function init(){
    var x=document.getElementsByTagName('a');
    for(var i in x){
        x[i].onclick=function(){
            return confirm('are you sure?');
        }
    }
}

Can be run successfully

But writing it in the following form cannot run correctly. return false is not captured. When cancel is clicked, the link still jumps. Why is this?

The functions are as follows:

window.onload=init;
function init(){
    var x=document.getElementsByTagName('a');
    for(var i in x){
        x[i].onclick=function(){
        ask();
        }
    }
}
function ask(){
    return confirm('are you sure?');
}

can be run correctly if written in the following form:

window.onload=init;
function init(){
    var x=document.getElementsByTagName('a');
    for(var i in x){
        x[i].onclick=ask;
    }
}
function ask(){
    return confirm('are you sure?');
}

Please tell me the difference between the three writing methods

世界只因有你
世界只因有你

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!