I have learned jquery for a while, but I have always been confused about the attributes of HTML, Val, and text, and their relationships and differences, below I will give you some examples to distinguish the relationship between jquery and the three.
In jquery, the relationship and difference between values of HTML, Val, and text attributes
First, there are two methods in the HTML attribute: one with a parameter and the other without a parameter.
1. No parameter HTML (): obtains the HTML content of the First Matching Element. This function cannot be used in XML documents. But it can be used in XHTML documents and returns a string
Example:
HTML page code: <div> <p> Hello </P> </div>
Jquery code: $ ("Div" ).html ();
Result: Hello.
2. html (VAL): sets the HTML content of each matching element. This function cannot be used in XML documents. But it can be used in XHTML documents. Returns a jquery object.
HTML page code: <div> </div>
Jquery code: $ ("Div" ).html ("<p> Nice To Meet You </P> ");
Result: [<div> <p> Nice To Meet You </P> </div>]
Second, there are two methods in the text attribute: one with a parameter and the other without a parameter.
1. No parameter text (): obtains the content of all matching elements. The result is a combination of text content contained by all matching elements. Returns a string
Example:
HTML page code: <p> <B> Hello </B> fine </P>
<P> thank you! </P>
Jquery code: $ ("p"). Text ();
Result: hellofinethankyou!
2. text (VAL): Set the text content of all matching elements, similar to HTML, however, encode HTML (replace "<" and ">" with the corresponding HTML Entity ). returns a jquery object.
HTML page code: <p> test paragraph. </P>
Jquery code: $ ("p"). Text ("<B> some </B> New text .");
Result: [<p> <B> some </B> New text. </P>]
Finally, there are two methods in the Val () attribute, one with a parameter and the other without a parameter.
1. No parameter Val (): obtains the current value of the First Matching Element. In jquery 1.2, the value of any element can be returned. Including select. If multiple values are selected, an array containing the selected values is returned.
Returns a string, array
Example:
HTML page code:
<P> </P> <br/>
<Select id = "single">
<Option> single </option>
<Option> single2 </option>
</SELECT>
<Select id = "multiple" multiple = "multiple">
<Option selected = "selected"> multiple </option>
<Option> multiple2 </option>
<Option selected = "selected"> multiple3 </option>
</SELECT>
Jquery code: $ ("p "). append ("<B> Single: </B>" + $ ("# single "). val () + "<B> multiple: </B>" + $ ("# multiple "). val (). join (","));
Result:[<P> <B> Single: </B> single <B> multiple: </B> multiple, multiple3 </P>]
2. Val (VAL): sets the value of each matching element. In jquery 1.2, this can also assign values to check, select, and radio elements, and return a jquery object.
HTML page code: <input type = "text"/>
Jquery code: $ ("input"). Val ("Hello world! ");
Result: Hello world!
A val () is applicable to the following page input elements:
Usage in jquery:
$ ("# Name"). Val () together with jquery, 17 jquery
1 text box:
<Input type = "text" id = "name"/>Content from 17 jquery
2 text fields:
<textarea id="textArea"/> 17jquery.com
3 drop-down box:
<Select id = "Address"> <option value = "0"> Beijing </option> <option value = "1"> Chengdu </option> <option value = "2 "> Kunming </option> </SELECT> jquery, 17 jquery
4 file boxes:
<input type="file" name="file" id="file"/> 17jquery.com
5 buttons:
<Input type = "button" id = "BT" value = "China"/> content from 17 jquery
Special cases:
6 single region:
<Input type = "radio" name = "sex" value = "0" checked/> male <input type = "radio" name = "sex" value = "1"/> female <input type = "radio" name = "sex" value = "2"/> demon17jquery.com
The jquery method is as follows:
//$('input[type=radio]:checked').val();//$('input[type=radio][name=sex]:checked').val();//$('input[type=radio][name=sex][checked]').val();//$('input[type=radio][@name=sex][checked]').val();//$('input[type=radio]@name=sex]:checked').val();$(':radio[name=sex]:checked').val(); 17jquery.com
7 check boxes:
<Input type = "checkbox" name = "love" value = "movie"/> movie <input type = "checkbox" name = "love" value = "book"/> books <input type = "checkbox" name = "love" value = "Learning" checked/> learning <input type = "checkbox" name = "love" value = "programming"/> programming together with jquery, 17 jquery
The jquery method is as follows:
VaR arr = []; // $ ('input [type = checkbox] [name = Love]: checked '). each (function () // all multiple boxes in the checked state $ (': checkbox [name = Love]: checked '). each (function () {arr. push (this. value) ;}); // arr. tostring (); jquery, 17 jquery
8-plus drop-down lists
<Select id = "address2" multiple> <option value = "0"> Beijing </option> <option value = "1"> Shijiazhuang </option> <option value =" 2 "> Baoding </option> </SELECT> 17jquery.com
The jquery method is as follows:
VaR arr = []; $ ('# address2> Option: selected '). each (function () {// directly use this to increase the speed. For example, remove the preceding: Selected, you can use the following method: // If (this. selected = true) {arr. push (this. value + "," + this. text );