jquery mobile。添加类时,点击按钮

问题描述:

嗨,我正在与JQM混合应用程序。 我想在按下或点按(按住)按钮时添加课程。jquery mobile。添加类时,点击按钮

这里是我的代码...

<a href='btnWhite on'>button</a> 

CSS

.btnWhite { background:gray } 
.btnWhite.on { background:black } 

jQuery Mobile的

$('.btnWhite').bind('touchstart', function() { 
$(this).addClass("on"); 
}); 

$('.btnWhite').bind('touchend', function() { 
$(this).removeClass("on"); 
}); 
+0

你到底是寻求帮助的是什么? – Jasper

工作例如:http://jsfiddle.net/Gajotres/LvhdG/

在这个例子中,我已经使用vmousedownvmouseupvmousecancel事件,这样我就可以测试它在桌面/移动设备的一致好评。只是touchstart取代他们,touchend,如果你想touchcancel,但它也将与VMOUSE事件工作。

HTML:

<!DOCTYPE html> 
<html> 
    <head> 
     <title>jQM Complex Demo</title> 
     <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/> 
     <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; minimum-scale=1.0; user-scalable=no; target-densityDpi=device-dpi"/> 
     <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" /> 
     <!--<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>--> 
     <script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>  
    </head> 
    <body> 
     <div data-role="page" id="index"> 
      <div data-theme="b" data-role="header"> 
       <h1>Index page</h1> 
      </div> 

      <div data-role="content"> 
       <a data-role="button" class="btnWhite">button</a> 
      </div> 
     </div>  
    </body> 
</html> 

的Javascript:

$(document).on('pagebeforeshow', '#index', function(){ 
    $(document).on('vmousedown','.btnWhite' ,function(){ 
     $(".btnWhite").addClass('on'); 
    }).on('vmouseup', function(){ 
     $(".btnWhite").removeClass('on'); 
    }).on("vmousecancel", function() { 
     $(".btnWhite").removeClass('on'); 
    }); 
}); 

CSS:

.btnWhite { 
    background:gray !important; 
} 
.on { 
    background:black !important; 
}