有这个html:
<div id='hi1'> aaa </div>
做的时候:
$('#hi1').sayHi();
我想收到一个说“嗨”的警报
有人知道这是怎么做到的吗?
有这个html:
<div id='hi1'> aaa </div>
做的时候:
$('#hi1').sayHi();
我想收到一个说“嗨”的警报
有人知道这是怎么做到的吗?
阅读有关jQuery 插件创作的更多信息。
$.fn.sayHi = function() {
alert($(this).text());
return this;
};
$('#hi1').sayHi();
阅读有关jQuery 插件的更多信息
jQuery 方法啊啊啊啊!
jQuery.fn.sayHi = function() {
// this is the jQuery object
// so this[0] is a DOM element (or undefined)
};
$.fn.extend({ sayHi:function(){ alert('hi'); });
您实际上想创建自己的方法(插件),称为:.sayHi()
$.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)
});