如何在 jQuery 中重置切换?

发布于 2024-09-29 17:00:43 字数 1844 浏览 1 评论 0原文

我的菜单结构如下所示。必要的要求是:

每个 li 必须是一个切换开关,可以选择和取消选择第 1-4 项。可以在1-4的范围内选择多个li。因此用户可以选择第 1 项和第 3 项。第 3 项及其背景都会突出显示。将鼠标悬停在任何项目和“所有”选项上时,应该有 mouseOver/mouseOut 背景更改。如果选择了某个 Item,则不应出现 mouseOver/mouseOut 的悬停状态。

最后,如果选择“全部”li,则其余项目(如果选择)应全部重置其切换状态并取消选择。然后,如果此后选择了另一个项目,则“全部”选择​​也应自行重置。

这是我到目前为止所得到的,我的悬停状态对所有组件都工作良好。我不知道如何在选择“全部”时编写重置所有项目 1-4 的解除绑定操作,以便重置项目 1-4 的“状态”,然后重置“全部”按钮(如果已选择),然后选择项目 1-4。

很抱歉说得啰嗦,但我想尝试尽可能最好地解释它,以免造成混淆。 :)

//css
.liselected{
background-color:#256ca0;
}

.lihoveron{
background-color:#eceef5;
}




$(document).ready(function() {

var startToggle = function(){
$('li[id|=nav]').toggle(
    function() {
        $(this).addClass('liselected').children().css('color','#ffffff');
        $(this).removeClass('lihoveron');
    },
    function() {
        $(this).removeClass('liselected').children().css('color','#5D788B');
    }).mouseover(function() {
        $(this).css('cursor','pointer');
    }).hover(function() {
        if ( $(this).hasClass('liselected') ){
        }
        else{
            $(this).addClass('lihoveron');
        }
    }, function() {
    $(this).removeClass('lihoveron');
});
};

startToggle();

});



<ul>
<li id="nav-all">
    <a class="item" href="">
        All Items
    </a>
</li>
<li id="nav-item1">
    <a class="item" href="">
        Item 1
    </a>
</li>
<li id="nav-item2">
    <a class="item" href="">
        Item 2
    </a>
</li>
<li id="nav-item3">
    <a class="item" href="">
        Item 3
    </a>
</li>
<li id="nav-item4">
    <a class="item" href="">
        Item 4
    </a>
</li>
<li id="nav-item5">
    <a class="item" href="">
        Item 5
    </a>
</li>

The menu structure I have is given below. The necessary requirements are:

Each li must be a toggle where it can be selected and deselected for items 1-4. Multiple li's can be selected between the range of 1-4. So a user could select Item 1 & Item 3 and their background would both be highlighted. Upon hovering over any of the Items and the "All" li selection items, there should be a mouseOver/mouseOut background change. If an Item is selected, then there should be no hover state of mouseOver/mouseOut.

Finally if the "All" li is selected, the rest of the Items (if selected) should all reset their toggle states and become deselected. Then if another Item is selected after that, the "All" selection should reset itself as well.

Here's what I've got so far and I have the hover state working fine for all the components. I don't know how to write the unbinding operation for the reset of all the Items 1-4 when "All" is selected so the 'state' of the Items 1-4 resets, and then also the reset for the "All" button if it is selected and then an Item 1-4 is chosen afterwards.

Sorry to be wordy but I want to try and explain it as best as possible so there is no confusion. :)

//css
.liselected{
background-color:#256ca0;
}

.lihoveron{
background-color:#eceef5;
}




$(document).ready(function() {

var startToggle = function(){
$('li[id|=nav]').toggle(
    function() {
        $(this).addClass('liselected').children().css('color','#ffffff');
        $(this).removeClass('lihoveron');
    },
    function() {
        $(this).removeClass('liselected').children().css('color','#5D788B');
    }).mouseover(function() {
        $(this).css('cursor','pointer');
    }).hover(function() {
        if ( $(this).hasClass('liselected') ){
        }
        else{
            $(this).addClass('lihoveron');
        }
    }, function() {
    $(this).removeClass('lihoveron');
});
};

startToggle();

});



<ul>
<li id="nav-all">
    <a class="item" href="">
        All Items
    </a>
</li>
<li id="nav-item1">
    <a class="item" href="">
        Item 1
    </a>
</li>
<li id="nav-item2">
    <a class="item" href="">
        Item 2
    </a>
</li>
<li id="nav-item3">
    <a class="item" href="">
        Item 3
    </a>
</li>
<li id="nav-item4">
    <a class="item" href="">
        Item 4
    </a>
</li>
<li id="nav-item5">
    <a class="item" href="">
        Item 5
    </a>
</li>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弃爱 2024-10-06 17:00:43

我会这样做:

  1. 你有一个包含所选元素索引的数组,即[2,23,5,1]。
  2. 当选择新元素时,函数需要检查数组是否具有最大允许元素。即 4 如果是,则删除第一个元素并添加新的单击元素。
  3. 那么您必须取消选择所有元素并选择数组中的元素。

第一次你有空数组:[]
在第一个选定的元素上 [2]
在第二个[2,23]
等等。

例如,当元素为 4 [2,23,5,1] 时,您单击了第 7 个元素,则从数组中删除 2 并在最后添加 7。
它看起来像: [23,5,1,7]

然后你做:

$('li.elementsClass').removeClass('liselected')

然后循环数组并添加数组中每个元素的类 liselected 。

华泰

I would do it something like this:

  1. you have an array with indices of selected elements i.e. [2,23,5,1].
  2. when a new element is selected a function need to check if the array has max allowed elements. i.e. 4 if so, remove the first element and add the new clicked.
  3. then you have to deselect all elements and make selection of the elements in the array.

in the first time you have empty array: []
on first selected element [2]
on the second [2,23]
and so on.

when elements are 4 [2,23,5,1] and you clicked for example on 7-th element then you remove 2 from the array and add 7 in the end.
it will look like: [23,5,1,7]

then you make:

$('li.elementsClass').removeClass('liselected')

and then loop the array and add class liselected of each element in the array.

HTH

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文