题
我的dom看起来像这样:
<div class="profile">
<a href="#"><img src="/img1.gif" /></a> <b>100</b>
<a href="#"><img src="/img2.gif" /></a>
</div>
好的,所以我的页面上有许多此类元素,因此使用jQuery我绑定了这样的所有事件:
$(".profile").live('click', myCallback);
var myCallback = function(event) {
// ??
};
现在,当某人单击第一个超链接时,我想将图像更改为“ /img1_on.gif”,然后对网页进行ajax调用,然后替换了一个值 返回值。
当单击另一个图像时,同一件事发生了,除了图像更改为“ /img2_on.gif”,然后转到另一个URL。
如果我转换了 进入DIV并添加一些CSS?
没有正确的解决方案
其他提示
$(".profile a").live("click", function(e) {
var temp = $("img", this);
var temp2 = temp.attr("src").split('.');
temp.attr("src", temp2[0] + "_on" + temp2[1]);
// Ajax goes here.
e.preventDefault();
});
您拥有它,以便DIV是被点击的DIV,而不是Div内部的链接。假设您填写了Ajax部分,此功能应该对您有用。
我不确定您要问什么,但是如果您想找到引起点击事件的元素,则可以使用e.target,然后从那里使用$(e.target)。这样的东西。
$(".profile").live('click', function(e){
if($(e.target).is("a"))
$(e.target).find("img").attr("src","/new/url.jpg");
});
例如,仅选择子链接(这个 将是链接):
$(".profile > a")
对于图像(这个 将是要操纵的图像):
$(".profile > a > img").live("click", function(event) {
var src = $(this).attr("src");
$(this).attr("src", src.replace(".gif", "_on.gif"));
// ajax call in any way you prefer
// in callback you can switch button state back
});
你会发现这个 邮政 确切地解释事件顺序是什么,以及在捕获或冒泡阶段聆听事件以及何时取消进一步的事件传播意味着什么。
不隶属于 StackOverflow