Home > Web Front-end > JS Tutorial > body text

Jquery extension method_jquery

WBOY
Release: 2016-05-16 18:27:54
Original
1002 people have browsed it

After searching for information on the Internet, there are two ways to write JQUERY extension methods, one is to use jquery.fn.extend, and the other is to use jquery.extend.
jquery.fn represents jquery.prototype, which adds methods to jquery objects. I just happened to use the extension method and use jquery.fn. Here I will write down the usage of jquery.fn. There are many of them on the Internet, which are very difficult to write
For example, when a button is clicked, a textbox value will pop up plus a parameter value

Copy code The code is as follows:

jquery.fn.extend({
alertMessage:function(message ){
var txtboxValue = $(this).val();//Use $(this) to indicate which object the extension method is added to, here refers to $('#textbox')
alert(txtboxValue message );
}
});
$(function(){
$("input[name='btn' ]").click(function(){
$(' #textbox' ).alertMessage("is a string");
})
})

html:
Copy the code The code is as follows:





So I dug out the Jquery Chinese document from the year before last. Browsed briefly about Jquery's methods. I discovered that Jquery is so powerful, why haven’t I discovered it before? So I personally wrote an extension function based on Jquery. The code is as follows:
Copy the code The code is as follows:

jQuery.fn.__toggleCheck = function (idPrefix) {
var c = false;
$(this).click(function () {
if (c) c = false;
else c = true;
$("input[type=checkbox][id^=" idPrefix "]").each(
function () {
this.checked = c;
}
);
});
};
jQuery.fn.__setRepeaterStyle = function (itemTag, evenStyle, hoverStyle) {
$("#" this.attr("id") " " itemTag ":odd").addClass(evenStyle);
var cssOriginal = "";
$("#" this.attr("id") " " itemTag "").mouseover(function () {
cssOriginal = $(this).attr("class");
$(this).addClass(hoverStyle);
});
$("#" this.attr(" id") " " itemTag "").mouseout(function () {
$(this).removeClass();
if (cssOriginal.length > 0) {
$(this).addClass (cssOriginal);
}
});
};

The above function is called as follows:
Copy code The code is as follows:


1
2
3
4
5
6
All

Check











1
1
1


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!