我决定学习Haxe来编译JavaScript,我面临的问题是关于如何使用该语言实现普通JavaScript功能的信息和示例很少。也许有人可以帮助我了解如何利用jQuery的每个函数,因为它似乎不起作用。编译“ js.html.Node没有字段宽度”时,它给我一个错误
这是代码。
import js.Lib;
import js.Browser;
import jQuery.*;
class Main {
static private var _jqSlider:JQuery;
static public function main():Void {
new JQuery(function():Void { //when document is ready
myFunc();
});
}
static private function myFunc() {
_jqSlider = new JQuery("aSlider");
_jqSlider.children().each(function(i,ele) {
trace(ele.width());
});
}
}
谢谢。我正在使用这些jQuery库http://lib.haxe.org/p/jQueryExtern,我尝试过trace(JQuery.cur.width()); 给我以下的班级没有场长
static private function myFunc() {
_jqSlider = new JQuery("aSlider");
trace( "hello" ); // works fine as console.log("hello")
_jqSlider.children().each(function(i,ele) {
var ele = new JQuery(ele);
trace( "hello" ); // Action Ignored
trace(ele.width()); // Action Ignored
});
}'
这是它输出到javascript的代码
(function (console) { "use strict";
var Main = function() { };
Main.main = function() {
$(function() {
Main.myFunc();
});
};
Main.myFunc = function() {
Main._jqSlider = $("aSlider");
console.log("hello");
Main._jqSlider.children().each(function(i,ele) {
var ele1 = $(ele);
console.log("hello");
console.log(ele1.width());
});
};
Main.main();
})(typeof console != "undefined" ? console : {log:function(){}});
这是一个JQuery问题。
根据jquery docs,ele
的确是普通元素,而不是JQuery对象,因此没有width()
方法。
您可以创建一个新的JQuery对象,如下所示:
var $ele = new JQuery( ele );
trace( $ele.width() ); // should work
或使用JQuery.cur
,将$( this )
在JS中转换为。
trace( JQuery.cur.width() );
参见http://api.haxe.org/js/JQuery.html
AFAIK,这两个解决方案是等效的,后者更简洁,可能更像“ jquery-like”。
不过,您似乎使用的不是官方的jQuery extern ,因为您导入的jQuery.*
不是js.JQuery
。
如果是这样,请提供有关您使用哪个库的更多信息。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句