简体   繁体   English

如何使用jQuery从最近的跨度中删除类?

[英]How can I remove a class from the span nearest span using jQuery?

I have a page that allows visitors to add favorites to a list. 我有一个页面,允许访问者将收藏夹添加到列表中。 It uses repeating rows to show items so generic classes are repeated. 它使用重复行来显示项目,以便重复泛型类。

How can I show a single modal window (.closest or .prev?) if they all have the same class? 如果它们都具有相同的类,如何显示单个模式窗口(.closest或.prev?)?

Here is my fiddle: http://jsfiddle.net/psasj74L/ 这是我的小提琴: http : //jsfiddle.net/psasj74L/

<span class="jsCrateMaxModal most-wanted-modal-container hide">
    <div class="most-wanted-modal">
        <div class="most-wanted-content">
            One.
            <div class="col-xs-12">
                <div class="btn-primary btn-block">OK</div>
            </div>
        </div>
    </div>
</span>
<button class="jsShowMaxModal mostWantedOff">Show Modal</button>

jQuery: jQuery的:

$('.jsShowMaxModal').click(function () {
    $(".jsCrateMaxModal").closest("span").removeClass("hide");
});

$('.most-wanted-modal-container').click(function () {
    $('.jsCrateMaxModal').addClass("hide");
});

You can use .prev() in this case to find the modal preceding the button. 在这种情况下,可以使用.prev()来查找按钮前面的模式。

$('.jsShowMaxModal').click(function () {
    $(this).prev(".jsCrateMaxModal").closest("span").removeClass("hide");
});

Working example: http://jsfiddle.net/psasj74L/2/ 工作示例: http : //jsfiddle.net/psasj74L/2/

However, this is a little flaky, and I would recommend wrapping the span and button in a div which offers some logical grouping. 但是,这有点不稳定,我建议将span和button包装在div中,该div提供一些逻辑分组。 If this is not possible, an alternative is to use data attributes to specify which modal a button should open. 如果这不可能,则替代方法是使用数据属性来指定按钮应打开的模式。

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

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