Javascript 的“this”问题关键词
我有以下 HTML 标记:
<a class="link1" href="javascript:load(0,1);">Click here</a><span class="loader"></span>
加载函数定义如下:
function load(flag,id){
/*Load Forum*/
if(flag==0){
$(this).next("span.loader").html("<img src='./images/loader.gif'/>");
console.log("Here");
//....Some code follows
所以我想要的是预加载器 gif 应在单击链接时出现在链接旁边。 “console.log”正在将“此处”打印到控制台,但图像没有出现。该图像位于正确的地址。我认为我使用“this”关键字的方式有问题。任何帮助将不胜感激。谢谢。 :)
更新:正如一些人指出的那样,我通过将 this 作为参数传递来传递对该元素的引用。现在这是我的代码,但它仍然无法工作:
<a class="link1" href="javascript:load(0,1,this);">Click here</a><span class="loader"></span>
function load(flag,id,ref){
/*Load Forum*/
if(flag==0){
if(ref){ $(ref).next("span.loader").html("123");console.log("Here"); }
I have the following HTML markup :
<a class="link1" href="javascript:load(0,1);">Click here</a><span class="loader"></span>
The load function is defined as follows :
function load(flag,id){
/*Load Forum*/
if(flag==0){
$(this).next("span.loader").html("<img src='./images/loader.gif'/>");
console.log("Here");
//....Some code follows
So what I want is the preloader gif should appear beside the link when it is clicked. The 'console.log' is printing 'Here' to the console but the image is not appearing. The image is located at the correct address. I think there is problem in the way I am using the 'this' keyword. Any help would be appreciated. Thanks. :)
Updated: As it was pointed out by some, I passed reference to the element by passing this as an argument. Here is my code now but it does not work still :
<a class="link1" href="javascript:load(0,1,this);">Click here</a><span class="loader"></span>
function load(flag,id,ref){
/*Load Forum*/
if(flag==0){
if(ref){ $(ref).next("span.loader").html("123");console.log("Here"); }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您使用 jQuery 绑定事件处理程序而不是使用“href”(无论如何这都是一个非常坏的习惯),那么您的
this
值将是正确的。这也意味着标记将会改变:
通过 jQuery 绑定处理程序意味着库将确保
this
引用正在为其调用处理程序的元素;即标记。按照您的方式(使用“href”伪 URL),情况并非如此。
If you bind your event handler with jQuery instead of using the "href" (which is a really bad habit anyway), then your
this
value would be correct.That also implies that the markup would change:
Binding the handler through jQuery means that the library will make sure that
this
refers to the element for which the handler is being invoked; that is, the<a>
tag. The way you have it (with the "href" pseudo-URL), that would not be the case.你的问题的答案是使用 javascript:load.call(this, 0, 1) 但 Pointy 是对的。
The answer to your question is to use javascript:load.call(this, 0, 1) but Pointy is right.
上下文是
window
因为您使用javascript:
伪协议引用它。我会不引人注目地附上它。上下文默认为锚点,因为我们不在全局上下文中执行它,因为匿名函数附加到 DOMElement (锚点节点)并且上下文通过
.call
传递。如果您需要 DOM 元素本身的参数,只需使用
data-
属性并使用.getAttribute
引用它们(假设它在 IE6 中一致工作)The context is
window
because you're referencing it with thejavascript:
pseudo protocol. I would unobtrusively attach it.The context defaults to the anchor since we're not executing it in global context, since the anonymous function is attached to the DOMElement ( anchor node ) and the context is passed via
.call
.If you need the parameters on the DOM element itself, just use
data-
attributes and reference them with.getAttribute
( assuming it works consistently in IE6 )尝试:
和
Try:
and
未经测试的
untested
Pointy 是对的,有更好的方法可以做到这一点,但如果您无法重构,您可以将
this
作为 javascript 调用的另一个参数传递...Pointy is right that there are better ways to do this, but if you can't refactor, you can pass
this
as another parameter of your javascript call...这是一个工作示例,假设您在多个位置使用
load(flag, id)
函数:http://jsfiddle.net/jfriend00/GRcXS/。主要问题是“this”的值是浏览器窗口,而不是使用 javascript: 分配事件处理程序时的元素。既然你有 jQuery,最好使用 jQuery 来连接点击事件,然后它会适当地设置this
点。HTML:
JavaScript:
Here's a working example that assumes you were using the
load(flag, id)
function multiple places: http://jsfiddle.net/jfriend00/GRcXS/. The main issue was that the value of "this" is the browser window, not the element when you assign the event handler using javascript:. Since you have jQuery, it's much better to use jQuery to hook up the click event and it will then set thethis
point appropriately.HTML:
Javascript: