2011-08-17 2 views
1

誰かが言うことができますが、WebViewのJQueryについての例はありません。私は多くの方法を試みたが成功しなかった。 これは簡単なコード例です。 CSSは動作しますが、JQueryは動作しません。JQueryは実際にWebViewで動作しますか?

1)インターネット許可が追加されました。

2)Javascriptが有効になっています。

3)エミュレータ1.6および2.2でテストされました。

public class UsingJQueryActivity extends Activity { 
    private WebView webView; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(final Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     webView = (WebView)findViewById(R.id.webView1); 
     webView.getSettings().setJavaScriptEnabled(true); 
     webView.loadUrl("file:///android_asset/js_please_run.html"); 
    } 
} 

「資産/ js_please_run.html」それはUIの一部であるとして

<html> 
<head> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.js" type="text/javascript"></script> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script> 
    <script> 
     alert('Hello world'); 
     confirm("Hello android");      
    </script> 
    <style> 
    div { 
     color: red; 
    } 
    </style> 
</head> 
<body> 
    <div> 
     All I hear is raindrops.Falling on the rooftop. Oh baby tell me why you have to go. 
     Cause this pain I feel you won't go away. And today, I'm officially missing you. 
    </div> 
</body> 
</html> 

答えて

2

WebViewは、デフォルトでは、ダイアログは表示されません、あなたは他の方法でJavaScriptをテストしたり、設定する必要がありますダイアログを示しWebChromeClient、この1つのようWebChromeClientを設定すると便利ですonJsAlert()

を参照してください。

public class DebugWebChromeClient extends WebChromeClient { 
    private static final String TAG = "DebugWebChromeClient"; 

    @Override 
    public boolean onConsoleMessage(ConsoleMessage m) { 
      Log.d(TAG, m.lineNumber() + ": " + m.message() + " - " + m.sourceId()); 

      return true; 
    } 

}