Jquery 把其他的都藏起来,但是

Jquery 把其他的都藏起来,但是,jquery,Jquery,代码如下: 我试图让

代码如下:

我试图让
按钮位于
隐藏/显示所有按钮下方的
按钮是,如果按下一个按钮,它将显示其文本,并隐藏其他文本

我使用了这段代码,但只要我点击任何按钮上的show,它就会显示并隐藏自己

$(".show").click(function() {
   $(this).parent().find("p").show(200);
   $("p").not(this).hide(200);
});​

帮助。

将显示代码更改为:

$(".show").click(function() {
    var container = $(this).closest(".itsawrap");
    $(".itsawrap").not(container).find("p").hide(200);
    container.find("p").show(200);
});​
在此进行工作演示:


这在容器级别工作,因此您可以对所需的容器进行操作。

将显示代码更改为:

$(".show").click(function() {
    var container = $(this).closest(".itsawrap");
    $(".itsawrap").not(container).find("p").hide(200);
    container.find("p").show(200);
});​
在此进行工作演示:

这在容器级别工作,因此您可以在所需的容器上操作。

基本上,您希望隐藏除当前区域之外的所有“itsawrap p”区域

$(".show").click(function() {
    var _p = $(this).parent().find('p');
    // maybe even do: $(this).closest('.itsawrap').find('p');
    // (if you ever think you'll wrap any of these things in other divs/etc

    _p.show(200);
    $('.itsawrap p').not(_p).hide();
});​

基本上,您希望隐藏除当前区域之外的所有“itsawrap p”区域

$(".show").click(function() {
    var _p = $(this).parent().find('p');
    // maybe even do: $(this).closest('.itsawrap').find('p');
    // (if you ever think you'll wrap any of these things in other divs/etc

    _p.show(200);
    $('.itsawrap p').not(_p).hide();
});​

您的问题是,show函数中的
不是
,而是按钮

$(".show").click(function() {
    var $thisP = $(this).parent().find("p")
    $thisP.show(200);
    $("p").not($thisP).hide(200);
});​

您的问题是,show函数中的
这个
不是
而是按钮

$(".show").click(function() {
    var $thisP = $(this).parent().find("p")
    $thisP.show(200);
    $("p").not($thisP).hide(200);
});​
像这样吗

$("#btn").click(function() {
    var oneshow = false;
    $(".para2, .para3, .para4").each(function() {
        if ($(this).is(":visible")) {
            oneshow = true;
        }
    });
    if (oneshow == true) {
        $(".para2, .para3, .para4").hide(200);
    } else {
        $(".para2, .para3, .para4").show(200);
    }
});

$(".hide").click(function() {
    $(this).parent().find("p").hide(200);
});

$(".show").click(function() {
    var p = $(this).parent().find("p");
    $(p).show(200);
    $(parent).not(p).hide(200);
});​
像这样

$("#btn").click(function() {
    var oneshow = false;
    $(".para2, .para3, .para4").each(function() {
        if ($(this).is(":visible")) {
            oneshow = true;
        }
    });
    if (oneshow == true) {
        $(".para2, .para3, .para4").hide(200);
    } else {
        $(".para2, .para3, .para4").show(200);
    }
});

$(".hide").click(function() {
    $(this).parent().find("p").hide(200);
});

$(".show").click(function() {
    var p = $(this).parent().find("p");
    $(p).show(200);
    $(parent).not(p).hide(200);
});​

问题出在
这个

$(".show").click(function() {
    var p = $(this).parent().find("p");
    p.show(200);
    $("p").not(p).hide(200);
});​

问题出在
这个

$(".show").click(function() {
    var p = $(this).parent().find("p");
    p.show(200);
    $("p").not(p).hide(200);
});​

最短的方法之一:

$(".show").click(function(e) {
    $("p").not($(this).parent().find("p").show(200)).hide(200);
});

最短的方法之一:

$(".show").click(function(e) {
    $("p").not($(this).parent().find("p").show(200)).hide(200);
});

错误是父项“this”是按钮,所以$(“p”)。不是(this)。隐藏(200);表示除按钮之外的所有p,按钮仍然是所有p

$(".show").click(function() {
     var parent = $(this).parent();
     $(".itsawrap").not(parent).find('p').hide(200);
     parent.find('p').show(200);
});​

错误是父项“this”是按钮,所以$(“p”)。不是(this)。隐藏(200);表示除按钮之外的所有p,按钮仍然是所有p

$(".show").click(function() {
     var parent = $(this).parent();
     $(".itsawrap").not(parent).find('p').hide(200);
     parent.find('p').show(200);
});​

这很棘手,也很有效,但它只会隐藏问题$(p)不是(这个)。隐藏(200)@MaximeMorin-我已经用一个更完整的解决方案更新了我的答案。非常感谢,我认为在脚本编写中顺序并不重要。@t2nite-如果这两条语句不是在同一个对象上运行,顺序并不重要,但是如果它们在同一个对象上运行,那么顺序确实重要。我目前的答案是将这两类对象分开,这样最后两行代码就不依赖于顺序,尽管通常先执行
.hide()
操作,然后再执行
.show()
操作更安全,因为如果有重叠,则更可能得到所需的结果。另外,请注意,
.closest()
.parent()
灵活一点,因为如果有人用另一个级别的容器调整HTML,它就不会中断。这很棘手,也很有效,但只会隐藏问题$(p)不是(这个)。隐藏(200)@MaximeMorin-我已经用一个更完整的解决方案更新了我的答案。非常感谢,我认为在脚本编写中顺序并不重要。@t2nite-如果这两条语句不是在同一个对象上运行,顺序并不重要,但是如果它们在同一个对象上运行,那么顺序确实重要。我目前的答案是将这两类对象分开,这样最后两行代码就不依赖于顺序,尽管通常先执行
.hide()
操作,然后再执行
.show()
操作更安全,因为如果有重叠,则更可能得到所需的结果。另外,请注意,
.closest()
.parent()
灵活一点,因为如果有人用另一个级别的容器调整HTML,它不会中断。
不引用
p
元素,因此
$(“p”)。而不是(此)
等同于
$('p')
并将选择页面上的所有
p
元素。您需要首先获取对按钮对应的
p
元素的引用。
不引用
p
元素,因此
$(“p”)。而不是(此)
相当于
$('p')
并将选择页面上的所有
p
元素。您想要的是首先获得对应于按钮的
p
元素的引用。