我有一些span标签,我希望能够点击它们并切换div。我不能把ID放在span标签上。我如何根据跨度上的标题来做这些独特的?这是我的HTML。
<span title="Silver" class="swatchColours swatchOneColour showPreview">
<span style="background-color:#cccccc;" class="swatchColour swatchColour_1"> </span>
</span>
<span title="Black" class="swatchColours swatchOneColour showPreview">
<span style="background-color:#000000;" class="swatchColour swatchColour_2"> </span>
</span>
<div id="colour_one">Silver</div>
<div id="colour_two">Black</div>
答案 0 :(得分:2)
$('span[title="Silver"]').click(function(){
$('#colour_one').toggle();
});
$('span[title="Black"]').click(function(){
$('#colour_two').toggle();
});
答案 1 :(得分:1)
$('span.swatchColours').on('click', function() {
var that = $(this);
if(that.attr('title') == 'Silver')
$('div#colour_one').toggle();
else if(that.attr('title') == 'Black')
$('div#colour_two').toggle();
});