0

このhtmlを持っている:

<div id='hi1'> aaa </div>

行うとき:

$('#hi1').sayHi();

「こんにちは」というアラートを受け取りたいのですが

誰かがこれがどのように行われるか知っていますか?

4

5 に答える 5

4

jQueryプラグインのオーサリングについてもっと読む。

于 2012-06-01T10:07:53.270 に答える
3
$.fn.sayHi = function() {
    alert($(this).text());
    return this;
};

$('#hi1').sayHi();

デモ

jQueryプラグインについてもっと読む

于 2012-06-01T10:09:33.727 に答える
2
$.fn.extend({ sayHi:function(){ alert('hi'); }); 
于 2012-06-01T10:08:00.777 に答える
2

jQueryメソッドaaaawええ!

jQuery.fn.sayHi = function() {
  // this is the jQuery object
  // so this[0] is a DOM element (or undefined)
};
于 2012-06-01T10:08:07.287 に答える
1

実際には、次のような独自のメソッド(プラグイン)を作成する必要があります。.sayHi()

デモjsFiddle

$.fn.sayHi = function(something){   
    alert(something);       
};

$('#hi1').sayHi('hi');    // 'hi' will be passed to the jQuery method 'sayHy'

詳細はこちら:http ://docs.jquery.com/Plugins/Authoring

もう一つの例:

$.fn.sayHi = function(txt, clr){   
     this.css({color: clr}).text(txt); // 'this' is your element delegated to your plugin
};

$('#hi1').click(function(){   
    $(this).sayHi('Heading is clicked!', 'red');  // (txt, crl)                                
});

デモ2

于 2012-06-01T10:11:58.547 に答える