Jquery - 无法使用 $(this) 定位正确的元素
可能的重复:
$(this) 在函数中不起作用
我遇到定位问题我的代码中的正确元素。我的页面上有一个缩略图列表,当您单击“我不喜欢这个”图标时,目标视频会更改为另一个缩略图。
HTML
<li class="videoBox recommended">
<div class="spacer" style="display: block;"></div>
<div class="features">
<div>
<a class="dislike_black" title="I dislike this" onclick="ThumbsDown(30835, 'relevance', '1');"></a>
</div>
</div>
<a href="...">
<h1>...</h1>
<div class="information">...</div>
</li>
Ajax 如下:
function ThumbsDown(id,sort,page) {
$.ajax({
url: "/change/videos/"+id+"/thumbsdown/",
type: "POST",
data: {
"sort": sort?sort:"",
"page": page?page:""
},
success: function(data) {
//$("#content .videoList ul li.videoBox").html(data); // THIS IS WORKING, but replaces ALL the divs
$(this).parent("li.videoBox").html(data); // Cant get this to work!
}
});
我做错了什么?甚至 $(this).css("border","1px Solid red") 也不能“工作”。 (我尝试了背景颜色和颜色)我什么也没看到。
$(This) 指的是调用函数的“a”标签,对吗?所以我正在寻找他的父母,名叫 videobox...帮忙吗?
那么我可以瞄准吗
Possible Duplicate:
$(this) doesn't work in a function
Im having a problem targeting the right element in my code. I have a list of thumbnails on my page, and when you click on a "I dislike this" icon the targeted video change for another one.
Here's the HTML
<li class="videoBox recommended">
<div class="spacer" style="display: block;"></div>
<div class="features">
<div>
<a class="dislike_black" title="I dislike this" onclick="ThumbsDown(30835, 'relevance', '1');"></a>
</div>
</div>
<a href="...">
<h1>...</h1>
<div class="information">...</div>
</li>
Ajax is:
function ThumbsDown(id,sort,page) {
$.ajax({
url: "/change/videos/"+id+"/thumbsdown/",
type: "POST",
data: {
"sort": sort?sort:"",
"page": page?page:""
},
success: function(data) {
//$("#content .videoList ul li.videoBox").html(data); // THIS IS WORKING, but replaces ALL the divs
$(this).parent("li.videoBox").html(data); // Cant get this to work!
}
});
What Iam doing wrong? even $(this).css("border","1px solid red") is not "working". (I tried with background color, and color too) I dont see anything.
$(This) refers to the "a" tag where the function is called right? So im searching for his parent named videobox... Help?
So can I target
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不应该使用
onclick
,您应该将其绑定到一个事件,特别是如果用户没有 ajax,您可以轻松地使其降级。将您的
标记更改为:
jQuery 事件:
现在它无需 javascript 即可工作,并且您没有使用令人讨厌的 onclick!未经测试/无保修。
You should not use
onclick
, you should bind it to an event, especially as you could easily make this degrade well if the user does not have ajax.Change your
<a>
tags to this:jQuery event:
Now it works without javascript, and you're not using nasty onclick! Untested/no warranty.
几乎。为了实现此功能,您需要将
context
传递给.ajax()
调用。像
上面的代码会导致所有ajax处理程序都将
this
指向document.body
。因此,您需要将document.body
替换为您要查找的元素。因此,如果您在某种点击处理程序
中调用您的函数,您只需调用context: this
,魔法就完成了。有关更多详细信息,请参阅 jQuery Ajax 文档 部分“上下文”。
Almost. In order to make this work, you need to pass in the
context
to the.ajax()
call.Like
The above code would cause that all ajax handlers will have
this
pointing todocument.body
. So you would need to replace thedocument.body
with the element you are looking for. So, if you for instance call your function in some kind of aclick handler
you could just callcontext: this
and the magic is done.See jQuery Ajax Doc section "context" for more details.
我相信您在原始代码中需要更改的就是在 success 函数之外获取对
this
的引用,如下所示:I believe all you needed to change in your original code was to get the reference to
this
outside of the success function, like this: