jQuery 如何在jquery中运行一个函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1191833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 10:52:37  来源:igfitidea点击:

How to run a function in jquery

jqueryfunction

提问by Corin

I'm a programming newbie, and I can't figure out how to store a function in JQuery and run in it multiple places.

我是一个编程新手,我不知道如何在 JQuery 中存储一个函数并在它的多个地方运行。

I have:

我有:

$(function () {
  $("div.class").click(function(){
    //Doo something

  });

  $("div.secondclass").click(function(){
    //Doo something
  });

});

Now the 2 "//Doo somethings" are the same, and I don't want to write the same code again.

现在这两个“//Doo somethings”是一样的,我不想再写同样的代码了。

If I put:

如果我把:

$(function () {

  function doosomething ()
  {
    //Doo something
  }

  $("div.class").click(doosomething);

  $("div.secondclass").click(doosomething);

});

That would run the function on page load, rather than only when it clicks.

这将在页面加载时运行该功能,而不是仅在点击时运行。

How do I do this correctly?

我该如何正确地做到这一点?

Thanks!

谢谢!

回答by Andrew Moore

The following should work nicely.

以下应该很好地工作。

$(function() {

  // Way 1
  function doosomething()
  {
    //Doo something
  }

  // Way 2, equivalent to Way 1
  var doosomething = function() {
    // Doo something
  }

  $("div.class").click(doosomething);

  $("div.secondclass").click(doosomething);

});

Basically, you are declaring your function in the same scope as your are using it (JavaScript uses Closuresto determine scope).

基本上,您是在与使用它相同的范围内声明您的函数(JavaScript 使用闭包来确定范围)。

Now, since functions in JavaScript behave like any other object, you can simply assign doosomethingas the function to call on click by using .click(doosomething);

现在,由于 JavaScript 中的函数的行为与任何其他对象一样,您可以简单地将其指定doosomething为单击时调用的函数,使用.click(doosomething);

Your function will not execute until you call it using doosomething()(doosomethingwithout the ()refers to the function but doesn't call it) or another function calls in (in this case, the clickhandler).

您的函数将不会执行,直到您使用doosomething()doosomething没有()引用该函数但不调用它)或另一个函数调用(在本例中为click处理程序)调用它。

回答by noboruwatanabe

I would do it this way:

我会这样做:

(function($) {
jQuery.fn.doSomething = function() {
   return this.each(function() {
      var $this = $(this);

      $this.click(function(event) {
         event.preventDefault();
         // Your function goes here
      });
   });
};
})(jQuery);

Then on document ready you can do stuff like this:

然后在准备好文档时,您可以执行以下操作:

$(document).ready(function() {
   $('#div1').doSomething();
   $('#div2').doSomething();
});

回答by Vincent Ramdhanie

function doosomething ()
{
  //Doo something
}


$(function () {


  $("div.class").click(doosomething);

  $("div.secondclass").click(doosomething);

});

回答by Vincent Ramdhanie

Alternatively (I'd say preferably), you can do it like this:

或者(我会说最好),你可以这样做:

$(function () {
  $("div.class, div.secondclass").click(function(){
    //Doo something
  });
});

回答by Joey Ezekiel

You can also do this - Since you want one function to be used everywhere, you can do so by directly calling JqueryObject.function(). For example if you want to create your own function to manipulate any CSS on an element:

您也可以这样做 - 由于您希望在任何地方都使用一个函数,您可以通过直接调用 JqueryObject.function() 来实现。例如,如果您想创建自己的函数来操作元素上的任何 CSS:

jQuery.fn.doSomething = function () {
   this.css("position","absolute");
   return this;
}

And the way to call it:

以及调用它的方式:

$("#someRandomDiv").doSomething();

回答by Steve

Is this the most obfuscated solution possible? I don't believe the idea of jQuery was to create code like this.There's also the presumption that we don't want to bubble events, which is probably wrong.

这是最模糊的解决方案吗?我不相信 jQuery 的想法是创建这样的代码。还有一种假设是我们不想冒泡事件,这可能是错误的。

Simple moving doosomething()outside of $(function(){}will cause it to have global scope and keep the code simple/readable.

简单地移到doosomething()外面$(function(){}将使其具有全局范围并保持代码简单/可读。