月度存档: 10 月 2012 - 第2页

haxe实现jquery(2)

JQuery原生代码:
[sourcecode]





This is id paragraph.

This is class paragraph.




[/sourcecode]
haxe实现:
[sourcecode]
package ;

import js.JQuery;
import js.Lib;
/**
* …
* @author amyflash.com
*/

class Main
{

static function main()
{
new JQuery(null).ready
(
function(e:JqEvent)
{
var domElem = new JQuery(“button”);
var clickHandlerFunction = function (e)
{
//隐藏class=”test”的元素
var d = new JQuery(“.test”);
d.hide();
//隐藏id=”test”的元素
var d2 = new JQuery(“#test”);
//d2.hide();
d2.css(“background-color”,”red”);//p背景变红色
}
domElem.bind( “click”, clickHandlerFunction );
}
);

}
}
[/sourcecode]
对应的html文件:
[sourcecode]




myjquery


This is id paragraph.

This is class paragraph.






[/sourcecode]

haxe实现jquery(1)

一直很讨厌js无用得顺手的ide,如果flashdevelop能让我写js,那就爽歪歪了
haxe可以做到,而且haxe里面的语法和as3类似,上手也挺快,今天就来研究下把w3school里面jquery教程,挨个移植到haxe实现,Let’s start!
原生jquery实现点击一个元素,然后隐藏它:
[sourcecode]




If you click on me, I will disappear.


[/sourcecode]
Haxe实现点击一个元素,然后隐藏它:
首先要安装haxejquery库:
haxelib install jquery
[sourcecode]
package ;

import js.JQuery;
/**
* …
* @author amyflash.com
*/

class Main
{

static function main()
{
new JQuery(null).ready
(
function(e:JqEvent)
{
var domElem = new JQuery(“p”);
var clickHandlerFunction = function (e)
{
JQuery.cur.hide();
}
domElem.bind( “click”, clickHandlerFunction );
}
);
}
}
[/sourcecode]
对应的html
[sourcecode]




myjquery


If you click on me, I will disappear.





[/sourcecode]
看起来是Main.hx编译成了myjquery.js,我的乖乖居然有98k,真要商业应用的话还是挺大的,需要优化再压缩,不过自己玩玩,或者对文件大小要求不严格而对时间要求很严格的商业应用的话,这个东东还是非常好的!

用haxe写javascript之ajax

[sourcecode language=”plain”]
//主类
class Test {

// 通过获取div的id改变div内容
static function setContent(id,content) {
var d = js.Lib.document.getElementById(id);
if( d == null )
js.Lib.alert(“Unknown element : “+id);
d.innerHTML = content;
}

// 创建一个链接
static function makeLink(title,code) {
return ‘‘+title+’‘;
}

// 点击链接以后触发的事件
static function click() {
//setContent(“main”,”Congratulations !”);
//同步加载
// setContent(“main”,haxe.Http.requestUrl(“data.xml”));
//异步加载
var r = new haxe.Http(“data.xml”);
//给链接加参数
//r.setParameter(“param”,”value”)
r.onError = js.Lib.alert;
r.onData = function(r) { setContent(“main”,r); }
r.request(false);//true for POST
}

static function main() {
// 初始化
setContent(“main”,makeLink(“click here”,”Test.click()”));
}
}
[/sourcecode]

对应的html文件
[sourcecode]

Haxe AJAX



[/sourcecode]

对应的data.xml
[sourcecode]
Hello World
[/sourcecode]