【问题标题】:Jquery call defined function from an outside function [duplicate]Jquery从外部函数调用定义的函数[重复]
【发布时间】:2017-01-21 02:29:18
【问题描述】:

嘿,我有一个像这样的 jQuery 函数:

$.fn.sexyForm = function (style){
    $('.sexyform').on('click',function(){
        $(this).find('input').trigger('focus');
    });

        var $input = $('.sexyform');
    ****************************************************
    ************* MORE CODE HERE TAKEN OUT *************
    ****************************************************
    function closeBox(element) {
       //console.log("Closing", element);
       //Get the element that needs to be closed
       var $el = $(element);
       //Grab the data we set on it
       var originalData = $el.data('original-styles');
       //console.log(originalData);
       var style = $el.data('style');
       //Reset the input

       $el.find('input').animate({
           left: originalData.startPosInput
       })
       $el.find('span').animate({
           width: originalData.startWidth,
           left: originalData.startPosPlace,
       })
   }
};

我正在尝试从另一个位置调用它:

function clearAll() {
    console.log('hi');
    $('.input3').val('');       
    $('.input3').removeClass('form-open');
    closeBox($('.input3'));
    $("span[data-id*='txt']").each(function(index) {
        //enable ALL textboxes
        $(this).css("pointer-events", "auto");
        $(this).fadeTo("fast", 1.0);
        $('#searchTop').text('above');
    });

    $('#schoolSelect').prop('selectedIndex', 0).selectric('refresh');
    $('#roles').prop('selectedIndex', 0).selectric('refresh');
    $('#clearBtn').focus();
}

错误来自 closeBox($('.input3'));。我尝试过以下操作:

$.fn.sexyForm.closeBox($('.input3'));

这也行不通。我错过了什么?

【问题讨论】:

    标签: javascript jquery function


    【解决方案1】:

    我可能不完全理解您在寻找@Stealth 的确切内容,但如果您想保留在sexyForm 中定义的closeBox 函数,另一种方法是传递某种形式的对象。例如:

    $.fn.sexyForm = function(style, BoxObj) {
        // Code here...
        BoxObj.closeBox = function(element) {
        // Any other code...
       }
    }
    

    根据 BoxObj 的定义位置,您可以选择将 BoxObj 作为参数传递给 clearAll:

    function clearAll(BoxObj) or () {
       // Code here
       BoxObj.closeBox($('.input3'));
       // More code 
    }
    

    【讨论】:

    • 这更像是我想做的事情。谢谢 RoboBear。
    【解决方案2】:

    closeBox 只定义在sexyForm 函数作用域内,不能在外部使用,可以尝试将其移到上作用域

    function close(element) {
      //console.log("Closing", element);
      //Get the element that needs to be closed
      var $el = $(element);
      //Grab the data we set on it
      var originalData = $el.data('original-styles');
      //console.log(originalData);
      var style = $el.data('style');
      //Reset the input
    
      $el.find('input').animate({
        left: originalData.startPosInput
      })
      $el.find('span').animate({
        width: originalData.startWidth,
        left: originalData.startPosPlace,
      })
    }
    
    $.fn.sexyForm = function(style) {
      $('.sexyform').on('click', function() {
        $(this).find('input').trigger('focus');
      });
    
      var $input = $('.sexyform');
      
      function closeBox(element) {
        close(element)
      }
    };
    
    
    function clearAll() {
      console.log('hi');
      $('.input3').val('');
      $('.input3').removeClass('form-open');
      close($('.input3'));
      $("span[data-id*='txt']").each(function(index) {
        //enable ALL textboxes
        $(this).css("pointer-events", "auto");
        $(this).fadeTo("fast", 1.0);
        $('#searchTop').text('above');
      });
    
      $('#schoolSelect').prop('selectedIndex', 0).selectric('refresh');
      $('#roles').prop('selectedIndex', 0).selectric('refresh');
      $('#clearBtn').focus();
    }

    【讨论】:

    • 但我需要它才能从另一个函数调用它......
    • “从另一个函数调用它”是什么意思?您可以使用我描述的方式从另一个函数调用它。您是否需要专门在sexyForm 中提供它?
    • 是的,我需要它是特别是性感的形式
    • 然后你可以在外部作用域中定义close函数,在sexyForm中定义调用closecloseBox函数。我已经更新了答案中的代码,试试看
    • close 必须是 inside $ .fn.sexyForm 并且我需要能够调用 关闭外部那个$.fn.sexyForm函数。
    猜你喜欢
    • 1970-01-01
    • 2011-02-18
    • 2010-12-12
    • 2015-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-22
    相关资源
    最近更新 更多