Haxe和jQuery Extern Library各函数

Haxe and jQuery Extern Library each function

我决定学习haxe来编译JavaScript,我面临的问题是关于如何使用这种语言实现plainJavaScript功能的信息和示例很少。也许有人可以帮助我了解如何利用 jQuery 每个功能,因为它似乎不起作用。编译时出现错误 "js.html.Node has no field width"

这是代码。

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() );给我下面的 Class has no field cur,

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 docsele 确实是一个普通元素,而不是 JQuery 对象,因此没有 width() 方法。

您可以像这样创建一个新的 JQuery 对象:

var $ele = new JQuery( ele );
trace( $ele.width() ); // should work

或使用JQuery.cur,这将在 JS 中转换为 $( this )

trace( JQuery.cur.width() );

http://api.haxe.org/js/JQuery.html

AFAIK,两种解决方案是等效的,后者更简洁,可能更 "jquery-like"。

您似乎使用了与 official one 不同的 jQuery extern,因为您导入 jQuery.* 而不是 js.JQuery

如果是,请提供有关您使用的库的更多信息。