Home > Web Front-end > JS Tutorial > Implement custom methods using the extended functionality provided by the JQuery library_jquery

Implement custom methods using the extended functionality provided by the JQuery library_jquery

WBOY
Release: 2016-05-16 16:36:46
Original
1143 people have browsed it

The JQuery wrapper provides a large number of methods that can be used directly in the page. However, no single library can meet all needs, so the JQuery library provides rich extension functions. Take disabling a group of form elements as an example to see how to add custom function extensions to the JQuery library simply and effectively. (JQuery has no way to disable form elements)

Upload code:

<!DOCTYPE> 

<html lang="en"> 

<head> 

<title>Custom Method!</title> 

<meta http-equiv="content-type" content="text/html;charset=utf-8"> 

<script type="text/javascript" src="js/jquery-2.1.1.js"></script> 

<script type="text/javascript"> 

jQuery(function() 

{ 

$("form input").disable(); 

}); 

</script> 

</head> 

<body> 

<p>测试自定义方法禁用表单元素</p> 

<form> 

<input type="text" value="test"><br/> 

<input type="button" value="confirm" class="test"> 

</form> 

</body> 

</html>
Copy after login

Text boxes and buttons have been disabled:

If you only disable the button, just add a css class;

jQuery(function() 
{
$("form input.test").disable(); 
});
Copy after login

Check if the button is disabled:

The implementation is also very simple. Open the jquery-2.1.1.js source code, which is not compressed. It is relatively easy. I introduced

is jquery-2.1.1.js, then add the following code

jQuery.fn.disable=function () { 

return this.each(function() 

{ 

if(this.disabled != null) this.disabled=true; 

}); 

}
Copy after login

Just call it according to the above case.

In the book "JQuery In Action", it is mentioned to expand the $.fn. method name (P12). I have tested it and it has not been disabled. Please correct me if I am wrong.

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