javascript - Haxe和jQuery Extern Library各函数

标签 javascript jquery haxe

我决定学习 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() );给我下面的类没有字段 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 确实是一个普通的 Element,而不是 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

据我所知,这两种解决方案是等效的,后者更简洁并且可能更“像 jquery”。

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

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

关于javascript - Haxe和jQuery Extern Library各函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33186135/

相关文章:

javascript - 函数中的回调似乎没有在 Require.JS 中执行

JavaScript:哪台机器进行计算?

javascript - 为什么单击按钮会触发焦点?

javascript - 我可以从 document.formID.submit(); 调用 JS 函数吗?

javascript - 使用时间作为 css 背景色

haxe - Haxe中主类的命名规则是什么?

javascript - 使用respond_to 做 |format| format.js 在模态中收藏照片后在模态中显示消息?

javascript - 如何仅在前一个请求完成后才触发另一个请求?

macros - 使用 Haxe 宏进行条件编译,而不是#if#end

macros - Haxe 生成带有参数的 super 调用