2016-04-13 26 views
1

我試圖調用按鈕的onclick的JavaScript函數。但它不起作用。onclick函數調用不工作在apache cordova(版本6.1.1)

我使用一些科爾多瓦插件

  1. sqlite的
  2. 文件
  3. 文件傳輸
  4. DBCOPY
  5. 文件路徑

    <button onclick="testalert()"> test alert </button> <script> function testalert() { alert("alert is working"); } </script> 
    

AndroidManifest.xml中

<?xml version='1.0' encoding='utf-8'?><manifest android:hardwareAccelerated="true" android:versionCode="1" android:versionName="0.0.1" package="io.cordova.gkslapp" xmlns:android="http://schemas.android.com/apk/res/android"> 
<supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<application android:hardwareAccelerated="true" android:icon="@drawable/icon" android:label="@string/app_name" android:supportsRtl="true"> 
    <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="@android:style/Theme.DeviceDefault.NoActionBar" android:windowSoftInputMode="adjustResize"><intent-filter android:label="@string/launcher_name"> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="23"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> </manifest> 

回答

0

我們用jQuery的委託函數調用document對象。 爲了讓代碼可讀,我們創建了一個只有委託功能的js fill,就像這樣。

$(document).delegate(".cus-name", "click", showLoader);

這裏showLoader是一個函數,它是在其他一些文件。使用委託函數的主要原因是因爲我們動態地加載頁面和頁面的一部分。

0

第一:所有在科爾多瓦/ PhoneGap的項目JS功能的應deviceready事件

二後執行:由於您使用jQuery的使用click函數來代替,這是一個更好的做法

0
**index.html** 

<!DOCTYPE html> 
<html> 
    <head> 
     <link rel="stylesheet" href="css/jquery.mobile-1.4.5.min.css" /> 
     <script src="js/jquery-2.0.1.min.js"></script> 
     <script src="js/jquery.mobile-1.4.5.min.js"></script> 
     <!-- 
     Customize this policy to fit your own app's needs. For more guidance, see: 
      https://github.com/apache/cordova-plugin-whitelist/blob/master/README.md#content-security-policy 
     Some notes: 
      * gap: is required only on iOS (when using UIWebView) and is needed for JS->native communication 
      * https://ssl.gstatic.com is required only on Android and is needed for TalkBack to function properly 
      * Disables use of inline scripts in order to mitigate risk of XSS vulnerabilities. To change this: 
       * Enable inline JS: add 'unsafe-inline' to default-src 
     --> 
     <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *"> 
     <meta name="format-detection" content="telephone=no"> 
     <meta name="msapplication-tap-highlight" content="no"> 
     <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width"> 
     <link rel="stylesheet" type="text/css" href="css/index.css"> 


    </head> 
    <body > 



     <div id="target"> 
      Click here 
     </div> 




     <script type="text/javascript" src="cordova.js"></script> 
     <script type="text/javascript" src="js/index.js"></script> 
    </body> 
</html> 



**index.js** 




$("#target").click(function() { 
    alert("Button Clicked"); 
}); 


Try this . 
+1

請閱讀[this](http://meta.stackoverflow.com/a/303605/4284627)瞭解僅有代碼的答案 –