jQuery 遍历 +现场事件处理程序
我在将实时事件处理程序附加到特定行时遇到一些问题。
我拥有的和我想要的:
我有一些 HTML,将在页面加载后动态生成,如下所示:
<table>
<tr>
<td></td>
</tr>
<tr>
<td class="bonus"></td>
</tr>
<tr>
<td></td>
</tr>
</table>
我想要两次 click< /code> events:
- 一个用于不是“奖励行”的行
- 一个用于后面有“奖励行”的行
我尝试过的内容和问题:
但是,我无法工作了解如何使用选择器来选择“后面有特定元素的元素”(即“前一个”选择器)。因此,我能得到的最好结果是:
- 不是“奖励行”的行:
$('tr:not(:has(.bonus))')
- 具有“奖励行”在他们之后:
$('tr + tr:has(.bonus)').prev()
这一切都很好,除非我使用 live()< /code> 通过遍历获得的 jQuery 对象的方法,而不是纯粹的选择,即
$('tr:has(.bonus)').prev().live('click', function() {
alert('hello');
});
我收到此错误:
未捕获的异常:语法错误, 无法识别的表达:)
问题作为一个更小的示例:
我希望这已本地化到我正在使用的某个脚本,但我已将其隔离到一个最小的 jsFiddle 示例,该示例仍然复制了该问题我: http://jsfiddle.net/ptvrA/
HTML:
<div></div>
<div id="target"></div>
JS:
$('#target').prev().live('click', function () {
alert('f');
});
似乎来自 此答案表明这是 live
的已知限制。
我的解决方法
作为参考,我的解决方法是:
- 以某种方式标记后面有“奖励行”的行
- 将
click
绑定到所有行,然后进行检查查看处理程序中它们后面是否有“奖励行”。
但如果我能得到一个“更好”的解决方案,即使是出于好奇,以防我在不同的情况下遇到这个问题,我也会很感激。
干杯
I'm having some issues with attaching live event handlers to particular rows.
What I have and what I'm after:
I have some HTML that will be generated dynamically after page load as follows:
<table>
<tr>
<td></td>
</tr>
<tr>
<td class="bonus"></td>
</tr>
<tr>
<td></td>
</tr>
</table>
I would like to have two click
events:
- One for rows that aren't a "bonus row"
- One for rows that have a "bonus row" after them
What I've tried and the problem:
However, I cannot work out how to use a selector to select "element that has a particular element after it" (i.e. a "previous" selector). As such, the best I can arrive at is:
- Rows that aren't a "bonus row":
$('tr:not(:has(.bonus))')
- Rows that have a "bonus row" after them:
$('tr + tr:has(.bonus)').prev()
This is all well and good, except whenever I use the live()
method on a jQuery object that was obtained through traversal, rather than pure selection i.e.
$('tr:has(.bonus)').prev().live('click', function() {
alert('hello');
});
I get this error:
uncaught exception: Syntax error,
unrecognized expression: )
The issue as an even more minimal example:
I was hoping this was localised to some script I am using, but I've isolated this to a minimal jsFiddle example which still replicates the issue for me: http://jsfiddle.net/ptvrA/
HTML:
<div></div>
<div id="target"></div>
JS:
$('#target').prev().live('click', function () {
alert('f');
});
It seems from this answer that this is a known limitation of live
.
My workarounds
For reference, my workarounds are either:
- Mark the rows that have a "bonus row" after them in some way
- Bind the
click
to all rows, and do a check to see if there is a "bonus row" after them within the handler.
But if I can get a "nicer" solution, even out of curiosity in case I run into this problem in a different situation, I'd appreciate it.
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将用 .live 方法做你想做的事。
对于所有将来使用谷歌到达这里的人。
发生这种情况是因为 .live() 使用为 jQuery 链中的第一个调用提供的原始选择器。它不考虑在初始 $('selector') 之后使用的其他方法。
Will do what you want with the .live method.
For all the people who get here in the future using google.
That is happening because .live() uses the original selector that was given to the first call in the jQuery chain. It doesn't no consider additional methods used after the initial $('selector').
老实说,我只是使用你的第二个想法并将单击绑定到所有行,然后检查下一行是否有奖金 td,如下所示:
小提琴位于此处: http://jsfiddle.net/7gdqc/2/
我不认为有一个纯粹的选择器方法可以做到这一点,这并不是真的解决方法 - 我将其称为解决您问题的有效解决方案。
To be honest, I'd just use your second idea and bind click to all rows, then check to see if next row has a bonus td in it, like this:
fiddle located here: http://jsfiddle.net/7gdqc/2/
I don't think there's a pure selector way to do it, and this isn't really a workaround - I'd call it a valid solution to your problem.