【问题标题】:How this.each(function() in jquery works?jquery 中的 this.each(function() 是如何工作的?
【发布时间】:2016-03-25 08:56:59
【问题描述】:

我正在尝试使用 Jquery 构建一个 Web 应用程序。我创建了一个函数并在全局范围内声明它,在调用它之后我正在使用 this.each(function () 并尝试执行我没有得到所需结果的函数要求。我也检查了语法并且没有收到任何错误。 以下是我调用该函数的代码:

$('[data-nitspagelabel]').click(function () { //click function on editable div to get editable buttons
            var nitsedit = $(this);
            var labeltype = $(this).data("nitslabeltype");
            if (labeltype == "text") {
                if (modal == false) {
                    modal = true;
                    $('[data-nitstextbutton]').css({ //popup text editing buttons 
                        'top': mouseY
                        , 'left': mouseX
                    }).fadeIn(400).click(function (e) {
                        var popupbox = $(this).attr('href');
                        openPopup(popupbox); // opens the editing tools popup
                        $(nitsedit).nitspopupeditor; // formatting buttons in action
                        $(nitsedit).attr('contenteditable', 'true');
                    });
                }
            }
        });

以下是我全局声明的函数

 $.fn.nitspopupeditor = function () { //Function to format editable items.
        this.each(function () {
            setTimeout(function () {}, 100);
            var $this = $(this);
            var selected_text = null;
            var uniquid = "nits" + new Date().getTime();

            $this.attr('data-nitsselect', uniquid); //set element unique id

我不知道我在哪里做错了,但是函数内部的东西不能正常工作。

【问题讨论】:

  • this 在你的函数里面是window,你想循环什么?
  • 你的意思是,我需要在$.fn.nitspopeditor 的第二行将this 替换为window
  • this 里面每个不是 eq window。据我所知,this 里面每个都是当前循环值。
  • 不,我的意思是this 将指向window 实例。
  • @VasylMoskalov 我说的是this 代码中的this.each

标签: jquery web-applications


【解决方案1】:

.each 是迭代器,它在每次迭代时获取数组的每个元素。 使用$this 可以访问当前元素,因此您应该向您的 .each 方法发送一个数组

这里是例子 http://api.jquery.com/jquery.each/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-10
    • 1970-01-01
    • 2011-02-12
    • 1970-01-01
    • 2012-10-17
    • 1970-01-01
    • 2012-01-26
    相关资源
    最近更新 更多