2014-01-13 27 views
3

在以前的項目工作,我曾與下面的代碼沒有問題,使用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"> 

我在這裏錯過了什麼?

+2

它工作正常。確保你將事件綁定到'pagecreate',如[demo](http://jsfiddle.net/Palestinian/tPNNv/)。另外,使用jQM 1.4樣式表而不是1.4RC1。 – Omar

+0

這就是我所缺少的,我沒有意識到你需要綁定到pagecreate以使touchstart工作。謝謝。 – OliverJ90

回答

0

我不確定它是否能解決您的問題,但不推薦使用.live()方法。嘗試使用.on()方法代替它。也可以嘗試用mousedown和mouseup替換touchstart和touchend,看看它是否有用。這是一個plunker where i got something similar to work

+0

我不認爲mouseup mousedown是正確的,因爲這是一種面向觸摸設備的設計,並且不涉及光標。奇怪的是,我似乎無法得到背景顏色屬性工作,我認爲問題在於,如果背景顏色屬性未被應用腳本來改變touchstart touchend背景顏色顯然不會工作。 – OliverJ90