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

Summary related to JQuery element attribute control

巴扎黑
Release: 2017-06-20 16:26:20
Original
1083 people have browsed it

JQueryElementAttributeControl

The code is as follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<script src="js/jquery-1.3.2.js" ></script> 
<script type="text/
javascript
"><!-- 
    $(function(){ 
             $("#aAttr1").click(function(){ 
                    $(    ":text").attr("value",$("#pTest").attr("id"));                 
                }) 
             $("#aAttr2").click(function(){ 
                    $(    ":text").attr("value",function(){ 
                                                             
return
 $("#pTest").attr("id");         
                                                     });                 
                }) 
             $("#aAttr3").click(function(){ 
                    $(    ":text").attr({value:"test2"}); 
                }) 
             $("#aAttr4").click(function(){ 
                    $("#inputTest2").removeAttr("value");     
                }) 
    }) 
// --></script> 
<title>无标题文档</title> 
</head> 
<body> 
<form> 
<input id="inputTest1" type="text" /> 
<input id="inputTest2" type="text" value="test" /> 
<p id="pTest">123</p> 
<a href="#" id="aAttr1">在text表单中显示p的ID方法1</a>| 
<a href="#" id="aAttr2">在text表单中显示p的ID方法2</a>| 
<a href="#" id="aAttr3">在text表单中显示test2</a>| 
<a href="#" id="aAttr4">去除inputTest2的value属性</a>| 
<input type="reset" /> 
</form> 
</body> 
</html>
Copy after login


1.element.attr(name)
Description : Used to get the name attribute value of a certain element. For example, $("#pTest").attr("id") in the example can get the ID value of pTest.
2.element.attr(name, value)
Description: Used to set the name attribute value of an element, such as $(":text").attr("value",$("# in the example) pTest").attr("id")) assigns the ID value of pTest to the value value of the text form.
Note: In the example (":text") is used to obtain the elements whose input form type is text. Similarly, other forms can also be obtained in the same way, such as You can use $(":button") to get it, and its return value is Array(Element). You can also use $(":input") to get all input elements. . To get and set the text and value of an element in JQuery, you can also use element.text()/element.text(value), element.val()/element.val(Value), the usage is the same as element.html() , if you have any questions, you can leave a comment and I will explain

3.element.attr(name,function)
Description: Used to set the name attribute value of an element is similar to the previous one, except here The value can be written as a function, which is more flexible.
4.element.remove(name);
Description: Used to remove the name attribute of an element.

The above is the detailed content of Summary related to JQuery element attribute control. For more information, please follow other related articles on the PHP Chinese website!

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