使用基于span标签标题的onclick

时间:2012-05-02 23:22:41

标签: jquery

我有一些span标签,我希望能够点击它们并切换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>

2 个答案:

答案 0 :(得分:2)

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


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

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

答案 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();           

});