开发者

How do i create a method/function in Jquery

开发者 https://www.devze.com 2023-02-05 02:40 出处:网络
How do i create a method in Jquery For ex开发者_开发技巧ample function dosomething() { // do something

How do i create a method in Jquery

For ex开发者_开发技巧ample

function dosomething()
{
 // do something 
}
dosomething();// i can call the function this way

How can i define function like dosomething() and call them in jquery?

Thanks


In exactly the same way. jQuery is JavaScript.

function doSomething() {
    // do something
}
$(function () {
    doSomething(); // call doSomething on document ready.
});


You can create a basic jQuery function:

(function($)
{
  $.fn.myFunc = function()
  {
    return this.each(function()
    {
      alert("do something for each element return by JQuery object");
    });
  };

})(jQuery);

Doing the above allows me to $("#myElement").myFunc();

Don't forget that jQuery is javascript. Javascript is not jQuery.


You can read up on jQuery plugin authoring here

0

精彩评论

暂无评论...
验证码 换一张
取 消