简体   繁体   English

如何更改/删除兄弟姐妹方法

[英]How to change/remove the siblings method

I grabbed this code when I was searching other questions, but when I tried to fit it to my needs I hit a problem. 当我搜索其他问题时,我抓住了这段代码,但是当我尝试使其适合自己的需求时,我遇到了问题。 As my links are spread across two parent divs, it doesn't interact correctly with the other siblings. 由于我的链接分布在两个父div上,因此无法与其他同级对象正确交互。

    var make_button_active = function () {
    //Get item siblings
    var siblings = ($(this).siblings());
    //Remove active class on all buttons
    siblings.each(function (index) {
        $(this).removeClass('active');
    });


    //Add the clicked button class
    $(this).addClass('active');
}

$(document).ready(function () {
    $(".clicked").click(make_button_active);
});

Here is the Fiddle 这是小提琴

If you click Link 1 and then click Link 6 for example, Link 1 stays highlighted when Link 6 highlights also. 例如,如果单击“链接1”,然后单击“链接6”,则当“链接6”也突出显示时,“链接1”保持突出显示。

Link 1-3 interact with each other and 4-6 do as well, but separately. 链接1-3彼此交互,而链接4-6彼此交互,但分别进行。 How cna I get them to all talk to each other and highlight on/off? 我如何才能让他们彼此交谈并突出显示开/关?

Updated your Fiddle . 更新了您的小提琴 Initially, you were only selecting the siblings (implies all under one parent). 最初,您仅选择兄弟姐妹(所有兄弟姐妹都隐含在同一个父母中)。 But you need to select across parents and hence siblings would not work. 但是您需要跨父母进行选择,因此siblings将无法工作。 Instead you have to use a generic identifier - .clicked in this case. 相反,在这种情况下,您必须使用通用标识符.clicked

Just change the following line: 只需更改以下行:

var siblings = ($(this).siblings());

to: 至:

var siblings = $('.clicked');

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

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