jQuery选择菜单显示具有相同ID的兄弟姐妹隐藏其他兄弟姐妹 [英] jQuery select menu show siblings with same ID hide other siblings

查看:442
本文介绍了jQuery选择菜单显示具有相同ID的兄弟姐妹隐藏其他兄弟姐妹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个选择菜单,该菜单根据ID显示或隐藏列表项.除了我的列表中有多个具有相同ID值的LI.

I have a select menu that shows or hides list items depending on ID. Except my list has multiple LI's with the same ID value.

这仅显示列表中具有匹配ID值的第一个LI.

This only shows the first LI in the list with the matching ID value.

$(function() {
    $("#orientation").change(function() {
        if (this.value == 'all') {
            $("#images").children().show();
        }
        else {
            $("#" + this.value).show().siblings().hide();
        }
    });
    $("#orientation").change();
});​

http://jsfiddle.net/dRqRV/

如何显示具有相同ID值的所有同级LI?

How can I show all sibling LI's with the same ID value?

推荐答案

ID必须唯一,jQuery仅选择具有特定ID的第一个元素,您可以将ID更改为类和代码:

IDs must be unique, jQuery only selects the first element with a specific ID, you can change ID to classes and code:

$(function() {
    var $li = $('#images').find('li')
    $("#orientation").change(function() {
        if (this.value == 'all') $li.show();
        else $li.hide().filter("." + this.value).show();
    }).change();
});

http://jsfiddle.net/Z3Qgz/

这篇关于jQuery选择菜单显示具有相同ID的兄弟姐妹隐藏其他兄弟姐妹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆