jQuery notes --text / html / val / attr / prop

Editor:

jQuery notes three --text / html / val / attr / prop

 

1. Obtain content

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
<p id="test">This is some <b>bold</b> text in a paragraph.</p>
<p>Name: <input type="text" id="test2" value="请输入..."></p>

$("#test").text() ------ This is some bold text in a paragraph.

$("#test").html() ----- This is some <b>bold</b> text in a paragraph.

$ ( "# Test2"). Val () ------ Enter ... (here is the predefined value)

2. Obtain property

the jQuery  attr ()  method for obtaining the attribute value

<p><a href="http://www.w3cschool.cc" id="w3s">W3Cschool.cc</a></p>

$("#w3s").attr("href") ------ http://www.w3cschool.cc

3. Set the contents and attributes

Or the above text, html, val, attr.

attr simultaneously setting a plurality of properties:

$("#w3s").attr({
  "href" : "http://www.w3cschool.cc/jquery",
  "title" : "W3Schools jQuery Tutorial"
});

And prop the difference 4.attr

Compared attr, prop 1.6.1 is out until the new, both from Chinese to understand the meaning, are getting / setting properties (attributes and properties). Only, or document window using .attr () method does not function properly before jQuery1.6, because the document window, and can not have attributes. prop came into being.

Guess you like

Origin blog.csdn.net/hello_world123456789/article/details/88948653