简体   繁体   English

如何根据点击值jQuery显示隐藏内容?

[英]How to show hidden content according to clicked value jquery?

sorry for the midunderstanding. 对不起,我很抱歉。 I meant index, not value. 我的意思是索引,而不是价值。 Sorry. 抱歉。

I am wondering if there is a way to use the value of the shown content ".wbox" of this jsfiddle example to coincide with the hidden value, that when clicked will show the hidden content? 我想知道是否有一种方法可以使用此jsfiddle示例中显示的内容“ .wbox”的值与隐藏的值重合,当单击该按钮时将显示隐藏的内容?

For example, When Cont 1 is clicked, hidden box 1 shows. 例如,单击“继续1”时,将显示隐藏的框1。 When Cont2 is clicked, hidden box 2 shows... and so forth. 单击Cont2时,隐藏的框2显示...,依此类推。

Here is my fiddle: http://jsfiddle.net/kqbLtn8b/1/ 这是我的小提琴: http : //jsfiddle.net/kqbLtn8b/1/

HTML: HTML:

<div class="box">
    <div class="content one">
        <h1>Cont 1</h1>
    </div>
    <div class="content two">
        <h1>Cont 2</h1>
    </div>
    <div class="content three">
        <h1>Cont 3</h1>
    </div>
</div>

<div class="hidden-content">
    <div class="hidden-box b-one">one</div>
    <div class="hidden-box b-two">two</div>
    <div class="hidden-box b-three">three</div>    
</div>

jquery: jQuery的:

var boxVal = $('.box').val();

Thanks for any help! 谢谢你的帮助!

What I am really trying to do is shorten the code from something like this: 我真正想做的是从这样的代码中缩短代码:

$('.one').on('click', function(){
    $('.b-one').show()
});

and so forth with the rest 其余的等等

Try this : use index of content div to show hidden-box 试试这个:使用content div的索引显示hidden-box

$(function(){
    $('.content').click(function(){
      var index = $(this).index();
        $('.hidden-content .hidden-box:eq('+index+')').show();
    });
});

And make change in your css, instead of hiding hidden-content div you need to hide hidden-box . 并在css中进行更改,而不是隐藏hidden-content div,您需要隐藏hidden-box So change your 所以改变你的

.hidden-content{
    display:none;
}

to

.hidden-box{
    display:none;
}

Demo 演示版

This is another way (although not so reliable, as it would break if you change your classes): 这是另一种方式(尽管不那么可靠,因为如果您更改类,它可能会中断):

 $(function () { $('.content').on('click', function () { var className = $(this).attr('class').replace('content', '').trim(); $('.hidden-box').hide(); $('.b-' + className).show(); }); }); 
 .hidden-box { display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="box"> <div class="content one"> <h1>Cont 1</h1> </div> <div class="content two"> <h1>Cont 2</h1> </div> <div class="content three"> <h1>Cont 3</h1> </div> </div> <div class="hidden-content"> <div class="hidden-box b-one">one</div> <div class="hidden-box b-two">two</div> <div class="hidden-box b-three">three</div> </div> 

UPDATE 更新

Based on @Regent comment which I agree to, this would be a more reliable way because it will work even if you change your markup. 基于我同意的@Regent注释,这将是一种更可靠的方法,因为即使您更改标记也可以使用。 You just need to add a data attribute to your markup that will be used to match elements: 您只需要在标记中添加一个data属性即可用于匹配元素:

 $(function () { $('.content').on('click', function () { var sel = $(this).data('rel'); $('.hidden-box').each(function () { $(this).toggle($(this).data('rel') == sel); }); }); }); 
 .hidden-box { display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <div class="box"> <div class="content one" data-rel="1"> <h1>Cont 1</h1> </div> <div class="content two" data-rel="2"> <h1>Cont 2</h1> </div> <div class="content three" data-rel="3"> <h1>Cont 3</h1> </div> </div> <div class="hidden-content"> <div class="hidden-box b-one" data-rel="1">one</div> <div class="hidden-box b-two" data-rel="2">two</div> <div class="hidden-box b-three" data-rel="3">three</div> </div> 

If you want to stick to the current HTML you have, it is going to be cumbersome and dirty since there are 2 ways to handle that scenario. 如果您要坚持使用当前的HTML,那么它将变得既麻烦又肮脏,因为有两种方法可以处理这种情况。

  1. Translate a string like "Cont 1" into "one", "Cont 2" into "two". 将类似“续1”的字符串转换为“一个”,将“续2”的字符串转换为“两个”。 It's all well and good till nine but what about 100 -> hundred? 到九点都很好,但是100-> 100呢? Or even thousand? 甚至一千?
  2. The other approach is instead of naming your hidden boxes as "b-one", "b-two", you can name them "b-1", "b-2", "b-3". 另一种方法是将隐藏的框命名为“ b-one”,“ b-two”,而不是将其命名为“ b-1”,“ b-2”,“ b-3”。 That way you can just detect the clicked element and then wipe of the "Cont " part and then use the remainder of the string to get the hidden part's class. 这样,您可以检测到单击的元素,然后擦除“ Cont”部分,然后使用字符串的其余部分获取隐藏部分的类。

Both the above cases will still give you a very very dirty code since you have to get the .html() of the clicked element and stip of h1 tags. 上述两种情况仍然会给您非常脏的代码,因为您必须获取clicked元素的.html()h1标签的提示。

So my suggestion would be to follow the below method. 因此,我的建议是遵循以下方法。

HTML: HTML:

<div class="box">
    <div class="content one" rel="1">
        <h1>Cont 1</h1>
    </div>
    <div class="content two" rel="2">
        <h1>Cont 2</h1>
    </div>
    <div class="content three" rel="3">
        <h1>Cont 3</h1>
    </div>
</div>

<div class="hidden-content">
    <div class="hidden-box b-1">one</div>
    <div class="hidden-box b-2">two</div>
    <div class="hidden-box b-2">three</div>    
</div>

JS: JS:

$('.content').on('click', function(){
    var divNum = this.rel;

    $('.b-'+divNum).show();
});

I recommend restructuring your HTML in the following way: 我建议通过以下方式重组HTML:

<div class="box">
<div class="content" id= "c1">
    <h1>Cont 1</h1>
</div>
<div class="content" id= "c2">
    <h1>Cont 2</h1>
</div>
<div class="content" id= "c3">
    <h1>Cont 3</h1>
</div>

<div class="hidden-content">
<div class="hidden-box" id= "h1">one</div>
<div class="hidden-box" id= "h2">two</div>
<div class="hidden-box" id= "h3">three</div>    

then use this as your jquery code: 然后将其用作您的jquery代码:

$('.content').click(function(){
var num = $(this).attr('id').split("c")[1];
$("#h"+num).show();
});

and by the way, change your css too: 顺便说一句,也更改您的CSS:

.hidden-content{
/*    display:none;*/
}
.hidden-box{
width:35px;
height:35px;
border:1px solid black;
display:none;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM