简体   繁体   English

JavaScript无法在Android 4.4以上版本的Cordova应用中使用

[英]JavaScript not working in Cordova app on Android 4.4+

I builded a simple test app for Android with Cordova and Intel XDK, and it works perfectly in the emulator in Android 4.0, 4.2, 4.3, but the JavaScript code does not work neither in Android 4.4, nor 5.0, 5.1, 6.0, 7.0. 我使用Cordova和Intel XDK为Android构建了一个简单的测试应用程序,它在Android 4.0、4.2、4.3的仿真器中可以完美运行,但是JavaScript代码在Android 4.4、5.0、5.1、6.0、7.0中均不起作用。 Could someone help me to understand what is wrong ? 有人可以帮助我了解问题在哪里吗? Thanks. 谢谢。 Here is the code: 这是代码:

<body>

<script>
    function getText() {
        document.getElementById('replace_text').innerHTML = 'A simple line';
    }
</script>

<a href="#" onclick="getText()">Click here to replace text.</a>
<br />
<div id="replace_text">Here is the text to replace.</div>

</body>

Did you added this line in your MainActivity.java? 您是否在MainActivity.java中添加了这一行?

view.getSettings().setJavaScriptEnabled(true);

EDIT 编辑

Jquery. jQuery。 What you need to do is download the jquery and link local and add a script file where you add this: 您需要做的是下载jquery并链接到本地​​,并在其中添加脚本文件:

var getText = function () {
  $('#replace_text').html('A simple line');
};

var init = function () {
    $('#clickMeToDoSomething').on('click',getText)
};

$(document).ready(init());

And that will be your HTML then: 然后将是您的HTML:

<body>
    <a href="#" id="clickMeToDoSomething">Click here to replace text.</a>
            <br/>
            <div id="replace_text">Here is the text to replace.</div>
</body>
<script src="assets/js/jquery-3.2.0.min.js"></script>
<script src="assets/js/myScript.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM