jQuery select menu show siblings with same ID hide other siblings

蹲街弑〆低调 提交于 2019-12-24 09:18:08

问题


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.

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/

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


回答1:


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/



来源:https://stackoverflow.com/questions/13925034/jquery-select-menu-show-siblings-with-same-id-hide-other-siblings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!