Jquery review (d) of the text (), html (), val ()

Three simple and practical method for jQuery DOM operations:

text () - sets or returns the text content of the selected element

html () - Set or return the contents of the selected elements (HTML tags)

val () - Set the value of field, or return the form

$("#btn1").click(function(){
   $("#test1").text("Hello world!");
 });
 $("#btn2").click(function(){
   $("#test2").html("<b>Hello world!</b>");
 });
 $("#btn3").click(function(){
   $("#test3").val("Dolly Duck");
 }); 

Note: html () tag identifies the set value of the element

text (), html () and val () callback function

JQuery above three methods: text (), html () and val (), also has the callback function. Callback function of two parameters: the selected elements in the list of the index of the current element, and the original (old) value. Then the new value function returns a string that you want to use.

The following example shows text with a callback function () and html ():

$("#btn1").click(function(){
   $("#test1").text(function(i,origText){
     return "Old text: " + origText + " New text: Hello world!
     (index: " + i + ")"; 
   });
 });

 $("#btn2").click(function(){
   $("#test2").html(function(i,origText){
     return "Old html: " + origText + " New html: Hello <b>world!</b>
     (index: " + i + ")"; 
   });
 }); 

 

 

 

 

Guess you like

Origin www.cnblogs.com/kunmomo/p/11231287.html