最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How do I add an onclick event to html tags without id's using javascript? - Stack Overflow

programmeradmin0浏览0评论

I tried doing this but it didn't seem to work:

 window.onload = initAll;


    function initAll(){
    document.getElementsByTagName('a').onclick = clickHandler;
     }

    function clickHandler(){
        if(this.toString().indexOf("localhost") < 0) {
            confirmation = confirm("You are now leaving .  Please click 'ok' to continue to this site, or 'cancel' to stay in ");
                if (confirmation == false){
                    return false;
                }

        }

    }

I know I can getElementById and that works, but it doesnt work this way. Any help would be appreciated.

Thanks!

I tried doing this but it didn't seem to work:

 window.onload = initAll;


    function initAll(){
    document.getElementsByTagName('a').onclick = clickHandler;
     }

    function clickHandler(){
        if(this.toString().indexOf("localhost") < 0) {
            confirmation = confirm("You are now leaving http://soso.com.  Please click 'ok' to continue to this site, or 'cancel' to stay in http://soso.com");
                if (confirmation == false){
                    return false;
                }

        }

    }

I know I can getElementById and that works, but it doesnt work this way. Any help would be appreciated.

Thanks!

Share Improve this question edited Sep 13, 2010 at 21:51 Copas 5,9525 gold badges30 silver badges43 bronze badges asked Sep 13, 2010 at 21:49 GrahamGraham 1,1554 gold badges16 silver badges25 bronze badges 3
  • 2 Does 'document.getElementsByTagName' return an array? You may need to loop through that returned array and assign the handler? – danjah Commented Sep 13, 2010 at 21:52
  • 1 JQuery was designed specifically for this sort of work. Can you use Jquery? – StriplingWarrior Commented Sep 13, 2010 at 21:52
  • I was considering jquery to insert the onclick handler if there wasnt a simpler fix, yes. – Graham Commented Sep 13, 2010 at 22:31
Add a comment  | 

5 Answers 5

Reset to default 6

document.getElementsByTagName('a') returns a NodeList of DOM Elements. So for starters, you will have to iterate over them and attach a handler to each like this :

var links = document.getElementsByTagName('a');
for( var i=0,il = links.length; i< il; i ++ ){
 links[i].onclick = clickHandler;
}

If there are many elements, I suggest you read about event delegation and assign just one handler for everything.

function initAll() 
{
    var elements = document.getElementsByTagName('a'); // returns an array

    // add the handler to each element
    var n;
    for (n = 0; n < elements.length; ++n)
        elements[n].onclick = clickHandler;
}

That's because getElementsByTagName returns a NodeList. You cannot assign an event handler via the onclick property on a NodeList, only a single DOMElement.

Try:

 var elems = document.getElementsByTagName('a');
 for (var i = 0; i < elems.length; i++) {
    elems[i].onclick = clickHandler;
 }

You need to iterate over all of the elements returned by document.getElementsByTagName

var links = document.getElementsByTagName('a');
var i = links.length;
while ( i-- ) {
    links[i].onclick = clickHandler;
}
// ... rest of your code ...

I'd strongly suggest using JQuery. Something like this should do the trick:

$(function(){$('a').click(clickHandler);});
发布评论

评论列表(0)

  1. 暂无评论