JavaScript: Get Function calling HTML-Object
Hey,
yes, I know that with [funcname].caller
or arguments.callee.caller
you can get a reference to the function which called the actual function, BUT - the following scenario开发者_如何学Go:
<a href="#" onclick="return something()">Test</a>
Inside of something()
I have no chance to get to the A-Tag, even with the .caller
reference, unless I alter the script in the following way:
<a href="#" onclick="return something(this)">Test</a>
With "this" I'm passing my A-Tag reference to the function but is there a way to get the A-Tag reference without explicitly passing it to the function?
Well, I think this is doing fine, it's just one word of code and it doesn't harm.Using jquery you can do this way
$(function(){
$("body").delegate("a", "click", function(){
alert($(this).html());
});
});
//Both code will work for you
$(function(){
$('a').click(
function(){
alert($(this).html());
}
);
})
精彩评论