The method of jquery to get the content of the label: 1. Use the “$(‘#id’).val()” method to get the value of the label; 2. Use the “$(‘#id’).text()” method to get the middle of the label 3. Get the html content in the tag through the “$(‘#id’).html()” method.
Recommended: “JavaScript Basic Tutorial”
get value of label or content of element using jquery
$('#tag id').val() can be used to get the value of the tag, for example, the value of the input tag can be obtained with it; $('#tag id').text() can be used to get the text in the middle of the label, for example, <span>value 1</span> can be used to get the value 1; $('#tag id').html() can be used to get the html content in the middle of the tag, for example, <div id="div1"><span>123</span></div> can use $('#div1 ').html() gets <span>123</span>; $('#tag id').attr('attribute name') can be used to get the value of the specified attribute of the tag, for example, <a href="xxx.aspx">xxx</a> can be used with .attr( 'href') to get xxx.aspx. jQuery provides three methods to get content: text(), html() and val(), the first two of which can be used to solve this problem: $("label#userid").text(); // Preferred, get the text of the label $("label#userid").html(); // It can also be achieved, get all the html tags in the label tag, generally In this case, there is text in the label label, so it is equivalent to the above method
An example demonstration is given below: Use the above two methods to obtain the content of the label label, and pay attention to the difference between the final results
Create Html element
<div class="box"> <span>Click the button to get the content in the label:</span><br> <div class="content"> <label id="userid">Enter username</label><input type="text"> </div> <input type="button" value="Get the content in the label"> </div>
…
The post jquery how to get tag content first appeared on Lenix Blog .
This article is reprinted from https://blog.p2hp.com/archives/9130
This site is for inclusion only, and the copyright belongs to the original author.