admin管理员组文章数量:1331614
I have a function that accepts the element it needs to operate on as a parameter element
function changeColor(element){
$(element).find('.middleBox').each(function(){
$(this).//do some stuff that does not matter now;
});
}
and I call it this way
changeColor($(document)); //this applies it to the whole document
changeColor($('#sectionOne')); //this applies it to only part of the document
I want to change it from the format where it accepts its object as a param to this format. How do I do it
$('#sectionOne').changeColor();
$(document).changeColor();
I have a function that accepts the element it needs to operate on as a parameter element
function changeColor(element){
$(element).find('.middleBox').each(function(){
$(this).//do some stuff that does not matter now;
});
}
and I call it this way
changeColor($(document)); //this applies it to the whole document
changeColor($('#sectionOne')); //this applies it to only part of the document
I want to change it from the format where it accepts its object as a param to this format. How do I do it
$('#sectionOne').changeColor();
$(document).changeColor();
Share
Improve this question
asked Feb 26, 2011 at 1:40
twittertwitter
9,3057 gold badges24 silver badges20 bronze badges
2 Answers
Reset to default 6As Nikita said, you need to write a jQuery plugin. Here's a basic example, which should be enough for what you are trying to do:
(function($) {
$.fn.changeColor = function() {
return this.each(function() {
$(this).find('.middleBox').each(function() {
$(this).//do some stuff that does not matter now;
});
});
};
})(jQuery);
You want to create a jQuery plugin. jQuery folks provide a tutorial for that.
If you don't like reading much, you can practically copy-paste maxHeight example and replace logic inside.
版权声明:本文标题:javascript - how to turn a function from function(element) syntax to $(element).function() syntax - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742257872a2441962.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论