例如
<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时,相同的类名。 感谢
答案 0 :(得分:1)
首先,您不应在页面上复制id
,因此我重新构建了您的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();
});
答案 1 :(得分: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();
});
答案 2 :(得分: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();
});
});
答案 3 :(得分: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>
答案 4 :(得分:0)
<强>&GT;&GT; JSFIDDLE&lt;&lt;
我建议使用toggle而不是show,如下所示:
<强>的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>