简体   繁体   English

jQuery:last不适用于混合标签

[英]jQuery: last doesn't work with mixed tags

I am having html look like below: 我有html如下所示:

<td id="sample">
    <select>
        <option>
    </select>
    <br/>
    <select>
        <option>
    </select>
    <br/>
//...
</td>

I know we should be using div instead of table but our whole webpage is designed this way and it is kind of time-consuming to change it. 我知道我们应该使用div而不是表,但是我们的整个网页都是以这种方式设计的,因此更改它有点耗时。

What I am trying to do is add a event handler to the change of last select tag. 我想做的是将事件处理程序添加到上一个select标签的更改中。 I have tried: $("#sample > select:last") and $("#sample").find("select).last() but both of them tend to find the last select before br tag which means the first one. I can probably solve this issue with adding an attribute in my code but that will result in adding a global variable and an amount of code. Is there a cleaner solution? 我试过: $("#sample > select:last")$("#sample").find("select).last()但是它们都倾向于在br标签之前找到最后一个select,这意味着第一个一个,我可以在代码中添加一个属性来解决此问题,但这会导致添加一个全局变量和大量代码,是否有更干净的解决方案?

Edit: Sorry that I didn't understand my issue correctly and many thanks to all the comments. 编辑:很抱歉,我无法正确理解我的问题,非常感谢所有评论。 I guess my issue actually is I put the event handler in my document ready section and append more <select> in that event handler. 我想我的问题实际上是我将事件处理程序放在我的文档就绪部分中,并在该事件处理程序中附加了更多<select> So every time when I am trying to trigger the event, it always come to the original last element. 因此,每次我尝试触发事件时,它总是到达原始的最后一个元素。

My code looks like: 我的代码如下:

$('#sample > select:last').on('change', function() {
    $('#sample > select:first').clone().show().appendTo('#sample');
    $("<br/>").appendTo('#sample');
});

But although I kind of find the reason, I am still confused in solving it. 但是,尽管我找到了原因,但我仍然对解决它感到困惑。 Is there any solutions around this? 有什么解决办法吗?

You're dynamically creating your elements, therefore you need a dynamic event delegation: 您正在动态创建元素,因此需要动态事件委托:

Dynamic Delegation works like: 动态委派的工作方式如下:

 $("staticElement").on("someevent", "dynamicElement", function)

and it's used to attach an event to the parent with a renewal look for recently appended child Elements. 并且用于将事件附加到父项,并以新外观查找最近添加的子项Elements。
To explain, on execution time if you attach an event to some elements directly like: $(".foo").click(fn) or $(".foo").on("click", fn) it will not account for .foo elements added in a later time cause they were not in the .foo collection at the time. 解释一下,在执行时,如果将事件直接附加到某些元素上例如: $(".foo").click(fn)$(".foo").on("click", fn) ,它将不会被考虑对于稍后添加的.foo元素,因为它们当时不在.foo集合中。
By assigning the event to the static parent, the event Delegation will bubble inversely searching for children (that match the selector argument and are the initiators of the same event trough event.target ) that received that event. 通过将事件分配给静态父项,事件委托将反向搜索接收该事件的子级(与选择器参数匹配,并且是同一事件通过event.target的发起方)。

In your example: 在您的示例中:

// static Parent // evt ,    dynamic 
$("#sample").on("change",   "> select:last",   function() {
    $('#sample > select:first').clone().show().appendTo('#sample');
    $("<br/>").appendTo('#sample');
});

More details to your question here: http://api.jquery.com/on/#direct-and-delegated-events 有关您的问题的更多详细信息,请访问: http : //api.jquery.com/on/#direct-and-delegated-events

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM