0

例如

<div id="red">
<div class="one"></div>
<div class="two"></div>
<div class="three"></div>
</div>
<div id="blue">
<div id="sub" class="one"></div>
<div id="sub" class="two"></div>
<div id="sub" class="three"></div>
</div>

当我单击 .one 时,显示 #sub .one,相同的类名。谢谢

4

5 回答 5

0

>> JSFIDDLE <<

我建议使用切换而不是显示,如下所示:

jQuery

$(document).ready(function() {
    $("#blue div").hide();
    $("#red div").click(function() {
        var redClass = $(this).attr("class");
        $("#blue ." + redClass).toggle();
    });
});

HTML结构:

<div id="red">
    <div class="one">1</div>
    <div class="two">2</div>
    <div class="three">3</div>
</div>
<div id="blue" class="sub">
    <div class="one">1</div>
    <div class="two">2</div>
    <div class="three">3</div>
</div>​
于 2012-07-19T14:55:03.453 回答
0

尝试这个

<div id="red">
  <div class="one"></div>
  <div class="two"></div>
  <div class="three"></div>
</div>
<div id="blue">
  <div class="sub one"></div>
  <div class="sub two"></div>
  <div class="sub three"></div>
</div>

   $('.one').click(function(){
        $('.sub').hasClass('one').show();
    });
于 2012-07-19T14:34:56.347 回答
0

尝试这种方式,更少的代码,更多的工作:

jQuery:

$(function(){
  $('#red div').click(function(){//divs inside #red

    var takeClass = (this).attr('class');//takes clicked element's class

    //inside #blue div
    $('#blue .'+takeClass).show();//show's div which got clicked element's class
    // if clicked element's class is 'one', this will show '#blue .one'
  });
});

你的html:

<div id="red">
<div class="one"></div>
<div class="two"></div>
<div class="three"></div>
</div>
<div id="blue">
<div id="sub" class="one"></div>
<div id="sub" class="two"></div>
<div id="sub" class="three"></div>
</div>
于 2012-07-19T14:41:56.657 回答
0
<div id="red">
  <div class="one"></div>
  <div class="two"></div>
  <div class="three"></div>
</div>
<div id="blue">
  <div id="one" class="sub"></div>
  <div id="two" class="sub"></div>
  <div id="three" class="sub"></div>
</div>

$().ready(function(){
  $('.sub').hide();
  $('#red>div').click(function(){
    $('#' + $(this).attr('class')).show();
  });
});
于 2012-07-19T14:37:59.320 回答
0

首先,您不应该id在页面上重复 s,因此我重新构建了您的 HTML:

<div id="red">
    <div class="one"></div>
    <div class="two"></div>
    <div class="three"></div>
</div>
<div id="blue" class="sub">
    <div class="one"></div>
    <div class="two"></div>
    <div class="three"></div>
</div>

然后,您可以执行以下操作:

$('#red').on('click', 'div', function(ev){
    $('.sub div').hide();
    $('.sub .' + $(this).attr('class')).show();
});
于 2012-07-19T14:38:20.927 回答