1

我有一些跨度标签,我希望能够在其中单击它们并切换 div。我不能将 ID 放在 span 标签上。我如何根据独特的跨度上的标题来做到这一点?这是我的 HTML。

<span title="Silver" class="swatchColours swatchOneColour showPreview">
        <span style="background-color:#cccccc;" class="swatchColour swatchColour_1">&nbsp;</span>
</span>
<span title="Black" class="swatchColours swatchOneColour showPreview">
        <span style="background-color:#000000;" class="swatchColour swatchColour_2">&nbsp;</span>
</span>
<div id="colour_one">Silver</div>
<div id="colour_two">Black</div>
4

2 回答 2

2
$('span[title="Silver"]').click(function(){
    $('#colour_one').toggle();
});


$('span[title="Black"]').click(function(){
    $('#colour_two').toggle();
});​

http://jsfiddle.net/AlienWebguy/XYfZV/

于 2012-05-02T23:24:51.837 回答
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();           

});
于 2012-05-02T23:30:53.647 回答