简体   繁体   中英

javascript 'this' passed as value parameter

I had this row:

echo "<td style='width:11%' id='client_quote_tr' name='client_quote_tr' onclick='changeColorTaskClientQuote(this);'>".$r['task']."</td>";

What's the value of ' this '? I mean it gets the value of attribute name, or what else?

Debug it. Add a console.log line. Open up the console. Click on the td and see what appears.

function changeColorTaskClientQuote(obj) {
    console.log(obj);
}

You will see it passes a reference to the element you clicked on. Very handy.

jsFiddle Example

this , which is the context of the function's execution, is the element on which the event handler was attached. So in this precise case, this will be the td element.

If you want to use the name in the function, it's this.name .

'this' in an inline event handler will refer to the DOM element the event fired on, in this case your 'td' element. For more info have a look at http://www.quirksmode.org/js/events_early.html

Thistabletd HTML元素

This is a reference to the DOM element the event is executed on. In this case the <td> .

在您的情况下-引用TD DOM元素。

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