我有以下按钮
<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条件并触发我的事件? 谢谢!
答案 0 :(得分:5)
if ($(this).data("image") === "bg_images/2.jpg") {
// ...
}
答案 1 :(得分:3)
if($(this).data('image') == 'g_images/2.jpg' ) {
//do your stuff.
}
答案 2 :(得分:3)
if ( $(this).data('image') == 'bg_images/2.jpg' ) {
// ...
}
或
if ( $(this).attr('data-image') == 'bg_images/2.jpg' ) {
// ...
}