开发者

Using jQuery to set a var equal to a bunch of class's and then hide that variable

I have a bunch of views: .view-1, .view-2, view-3, etc... I was using something hacky lik开发者_如何学运维e this to get the right view showing when the right item in the navigation was clicked:

$(".contact a").click(function() {
    $(".view-1").hide();
    $(".view-2").hide();
    $(".view-3").hide();
    $(".view-4").hide();
    $(".view-5").hide();
    $(".view-6").show();
    return false; 
});

But this is difficult to maintain over a bunch of nav item and ever more views.

I'd like to do something like this:

 var $allViews = $(".view-1,.view-2");
    $(".how").click(function() {
        $(allViews).hide();
        $(".view-2").show(); 
        return false; 
    });

Where I could assign all the view classes


How about :

var $allViews = $("[class|='view']")

Gets all elements with classes equal to 'view' or starting with 'view' followed by a hyphen

Or:

var $allViews = $("[class^='view-']")

Gets all elements with classes that start with 'view-`

$allViews.hide() would hide all of those elements.

Example here: http://jsfiddle.net/Af9M3/


Why not make a function hideViews(views) { // views.hide(); } or even use the filter to hide the views except the clicked one? You could get at which view was clicked from the click event.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜