在以前的項目工作,我曾與下面的代碼沒有問題,使用touchstart和touchend事件修改按鈕的CSS:Touchstart和touchend不是在iOS上使用jQuery Mobile和科爾多瓦
<script>
$('input[type="button"]').on('touchstart', function(e){
$(this).css('background-color','#49D623');
});
$('input[type="button"]').on('touchend', function(e){
$(this).css('background-color','');
});
</script>
這是在一個iOS的Cordova項目使用版本2.9.0
因爲我已經升級到cordova 3.2,並使用jquery mobile 1.4.0與jquery 1.10.2和上面的代碼不再起作用。
我試過.on(touchstart
和.bind(touchstart
和.live(touchstart
與jquery 1.8一起工作,但在1.9中不推薦使用。我也嘗試使用按鈕的ID,但這也不起作用。
我知道touchstart正在被識別,因爲我已經測試過它來觸發其他功能,它的功能就像一個魅力,只是不適合這個。
這是按鈕如何出現在我的html:
<input type="button" id="submit" data-role="none">
相關的按鈕的CSS:
input[type="button"] {
-webkit-appearance: none;
border-radius: 0px;
width: 92%;
margin-top: 3%;
margin-left: 5%;
background-color: rgba(23,24,54,1.00);
padding: 10px;
border-width: 1px;
border-color:rgba(88,88,88,1.00);
font-size: 1em;
color: #FFFFFF;
font-family: 'HelveticaNeue-Light', 'HelveticaNeue', Helvetica, Arial, sans-serif;
background-image: url(../img/padlock2.png);
background-size: 20px;
background-repeat:no-repeat;
background-position: 50% 50%;
}
的下面是我的html文件的頭引用:
<script src="js/jquery-1.10.2.min.js"></script>
<script src="js/jquery.mobile-1.4.0.min.js"></script>
<script src="cordova.js"></script>
<script src="cordova_plugins.js"></script>
<link rel="stylesheet" type="text/css" href="css/jquerymobile1.4rc1.css">
<link rel="stylesheet" type="text/css" href="css/login.css">
<link rel="stylesheet" type="text/css" href="css/globalstyles.css">
我在這裏錯過了什麼?
它工作正常。確保你將事件綁定到'pagecreate',如[demo](http://jsfiddle.net/Palestinian/tPNNv/)。另外,使用jQM 1.4樣式表而不是1.4RC1。 – Omar
這就是我所缺少的,我沒有意識到你需要綁定到pagecreate以使touchstart工作。謝謝。 – OliverJ90