0

ive 以下按钮

<button class="image1-button" data-image="bg_images/1.jpg">button 1</button>
<button class="image1-button" data-image="bg_images/2.jpg">button 2</button>
<button class="image1-button" data-image="bg_images/3.jpg">button 3</button>
<button class="image1-button" data-image="bg_images/4.jpg">button 4</button>

使用以下代码

$(".image1-button").click(function() { 
var url = 'url(' + $(this).data('image') + ')';

$('#div1').css('background-image', url).fadeIn(500);

 // what to write in this if that it works...
 if('data-image="bg_images/2.jpg"') {
     here should something happen if button 2 is pressed
 }

我如何编写它进入并触发我的事件的 if 条件?谢谢你!

4

3 回答 3

5
if ($(this).data("image") === "bg_images/2.jpg") {
    // ...
}
于 2013-02-10T14:41:32.563 回答
3
if($(this).data('image') == 'g_images/2.jpg' ) { 
    //do your stuff.
}
于 2013-02-10T14:41:53.107 回答
3
if ( $(this).data('image') == 'bg_images/2.jpg' ) {
    // ...
}

或者

if ( $(this).attr('data-image') == 'bg_images/2.jpg' ) {
    // ...
}
于 2013-02-10T14:43:39.663 回答