如何传递对“this”的引用关于href javascript函数? [英] How pass reference to "this" on href javascript function?
问题描述
我有一个关于此href的链接:
I have a link with this href:
href="javascript:foo(this);"
当我称之为this指向窗口对象,而不是链接。如何传递对该链接的引用?
when I call it "this" points to the window object, not the link. How do I pass a reference to the link?
编辑备注:问题是如何通过href传递,而不是一般 - 我知道onclick!
Edit note: The question is how to pass with href, not generally - I know about onclick!
并且没有复制id和make getElementById,这不是this,它是对某些元素的DOM搜索,不需要在HTML中内联。
And not copying id and make getElementById, that's not "this", it's DOM search for certain element, no need to make it inline in HTML.
anwer是:不可能。
The anwer is: not possible.
推荐答案
当你使用javascript:.... 在一个href中,你将这个函数称为全局。不在链接的上下文中。
您可以尝试:
When you use "javascript: .... " in an href, you are calling this function globaly. Not in the context of the link. You can try with:
<a href="#" onclick="foo(this); return false;">MyLink</a>
这篇关于如何传递对“this”的引用关于href javascript函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!