简体   繁体   中英

Add onclick function to a href with a this parameter using Javascript

I have a href like this

<a title="my title text" id="6" href="#" class="someclass" onclick="deleteRows(this.parentNode.parentNode)">X</a>

Now I want to add a new exactly the same href but I have no idea how to add onclick with 'this' paraeter.

Tried those 2 but non of it is working.

var a = document.createElement('a'); // create DIV element
    a.onclick = function() {
        (a.parentNode.parentNode);
    };

and

var a = document.createElement('a'); // create DIV element
    a.onclick = function() {
        (this.parentNode.parentNode);
    };

Searched google but did not faound anything. How I should do it?

You forgot to call deleteRows inside your event listener:

var a = document.createElement('a');
a.onclick = function() {
    deleteRows(this.parentNode.parentNode);
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM