JavaScript和JQuery的不工作在Eclipse中Android模拟器 [英] JavaScript and JQuery does not works in eclipse android emulator

查看:87
本文介绍了JavaScript和JQuery的不工作在Eclipse中Android模拟器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < HTML>
< HEAD><冠军> ABC< /标题>
&所述;! - &所述;脚本的src =jquery.gamequery-0.7.0.js>&所述; /脚本&GT  - →;
<! - <脚本SRC =htt​​p://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js类型=文/ JavaScript的>< / SCRIPT&GT ;  - >
&所述;脚本的src =htt​​p://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js>&所述; /脚本>
<脚本SRC =htt​​ps://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js类型=文/ JavaScript的>< / SCRIPT>
<风格>
#abc {
    高度:200像素;
    宽度:200像素;
    左:x 400像素;
    背景:深蓝色;
    位置:绝对的;

}
#def {
    左:25%;
    顶部:25%;
    位置:绝对的;
}
#球{
    位置:绝对的;
    顶:100像素;
    左:180px;
    的line-height:80px;
    不透明度:0;
}

< /风格>
<脚本>
document.addEventListener(deviceready,onDeviceReady,真正的);
传播onDeviceReady(){
    //设置#abc的不透明度为0(淡出)
    $(#ABC)。CSS('不透明',0)
  //特效功能

  //创建淡出#abc效果
    $(#ABC)。动画({
      顶部:200像素,
      不透明度:'1'

    });

    //放大#def的
    $(#DEF)。动画({
        高度:75像素,
        宽度:75像素
    });

    //闪烁#abc效果
    $(#ABC)效应(跳动,{次数:3},100);

    //摇动#def效果
    $(#def).effect(换血,{时间:3},50);

    //#球转身#abc轨道
    变种角度= 0; //起始位置(度)
    VAR距离= 100; //从距离b的
    VAR速度= 300;在每秒度//转速
    变种率= 10; //刷新率以毫秒为单位

    函数f(){

        。VAR O = $('#高清)的偏移量();

        VAR T = o.top +(距离* Math.sin(角度* Math.PI / 180.0));
        变种L = o.left +(距离* Math.cos(角度* Math.PI / 180.0));

        $('#球)。CSS({
            顶:T,
            左:L
        });
        角+ =(速度*(速度/ 1000))%360;
     }
    的setInterval(F,速度);
    //让出现在#ball
    $(#球)的CSS('不透明度,1);

};

< / SCRIPT>
< /头>
<身体的onload =onDeviceReady()>
<按钮ID =btnTry>我试试< /按钮>
<按钮ID =btnReturn的onclick =window.location.reload();>恢复正常< /按钮>
< D​​IV ID =ABC>
< IMG SRC =像素/钻石%20Heart.gifID =高清/>
< / DIV>
< IMG SRC =像素/ Orange.gifID =球/>

< /身体GT;
< / HTML>
 


test.html的

 公共类BbbMainActivity延伸活动{
    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.bbb_main);
        的WebView WV =(web视图)findViewById(R.id.webview);
        wv.getSettings()setJavaScriptEnabled(真)。
        wv.loadUrl(文件:///android_asset/test.html);
            }
 

}

BbbMainActivity.java

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    工具:上下文=BbbMainActivity。>

    <的TextView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_centerHorizo​​ntal =真
        机器人:layout_centerVertical =真
        机器人:文本=@字符串/参考hello world/>
<的WebView的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / web视图
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
/>
 

bbb_main.xml

我的问题与此code是,我可以在Web浏览器中运行的code正确,但是当涉及到​​我使用的Eclipse(Android的2.1和放大器; Android 2.2的)设备仿真器来运行

才会知道什么是错我才做?

在code将呈现多效。随意将其复制到记事本并保存为html的并运行它。

解决方案

  WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(真正的);
 

<html>
<head><title>ABC</title>
<!--<script src="jquery.gamequery-0.7.0.js"></script>-->
<!--<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>-->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js" type="text/javascript"></script>
<style>
#abc{
    height:200px;
    width:200px;
    left: 400px;
    background: darkblue;
    position: absolute;

}
#def{
    left:  25%;
    top: 25%;
    position: absolute;
}
#ball{
    position: absolute; 
    top: 100px; 
    left: 180px;  
    line-height: 80px;
    opacity: 0;
}    

</style>
<script> 
document.addEventListener("deviceready", onDeviceReady, true); 
function onDeviceReady(){
    //set the opacity of #abc to 0(Fade)
    $("#abc").css('opacity', 0)
  //Effect Function 

  //Create fade out effect for #abc
    $("#abc").animate({ 
      top: '200px',
      opacity:'1'

    });

    //Enlarge of #def
    $("#def").animate({ 
        height: '75px',
        width: '75px'
    });

    //Blink effect for #abc
    $("#abc").effect("pulsate", { times:3 }, 100);

    //Shake effect for #def
    $( "#def" ).effect( "shake" ,{ times:3 }, 50);

    //#ball turn orbit around #abc
    var angle = 0;     // starting position (degrees)
    var distance = 100; // distance of b from a
    var speed = 300;    // revolution speed in degrees per second
    var rate  = 10;    // refresh rate in ms

    function f() {

        var o = $('#def').offset();

        var t = o.top + (distance * Math.sin(angle * Math.PI/180.0));
        var l = o.left+ (distance * Math.cos(angle * Math.PI/180.0));

        $('#ball').css({
            top: t,
            left: l
        });
        angle += (speed * (rate/1000)) % 360;
     }
    setInterval(f, rate);
    //Let the #ball to appear
    $("#ball").css('opacity',1);

};

</script> 
</head>
<body onload="onDeviceReady()">
<button id="btnTry">Try Me</button>
<button id="btnReturn" onclick="window.location.reload();">Return To Normal</button>
<div id="abc">
<img src="pix/Diamond%20Heart.gif" id="def" />
</div>
<img src="pix/Orange.gif" id="ball" />

</body>
</html>


test.html

public class BbbMainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bbb_main);
        WebView wv = (WebView) findViewById(R.id.webview);
        wv.getSettings().setJavaScriptEnabled(true);
        wv.loadUrl("file:///android_asset/test.html");
            }

}

BbbMainActivity.java

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".BbbMainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/>

bbb_main.xml

My problem with this code is that I can run the code properly in the web browser but when comes to the device emulator which I use eclipse (Android 2.1 & Android 2.2) to run.

Can I know what mistake did I made ?

The code will show multiple effect. Feel free to copy it into notepad and save it as .html and run it.

解决方案

WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);

这篇关于JavaScript和JQuery的不工作在Eclipse中Android模拟器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆