How to get the value of span in jquery

藏色散人
Release: 2020-11-17 10:22:51
Original
7423 people have browsed it

Jquery method to get span value: first create a front-end code example; then set span; finally pass "$(document).ready(function(){$("button").click(function() {..}}" method to get the value of span.

How to get the value of span in jquery

Recommended: "javascript basic tutorial"

Let’s take a look at an example first. The sample code is as follows:

    
aaaa
Copy after login

The result of alert is as follows

[Value of val: undefined]

[Value ofhtml:null]

text value:

None of the above three methods got the desired value. The reason why they didn’t get it was becausehtml is parsed from the topdown. When parsing to $("#spId") in js, the span below does not exist yet. , of course you can’t get it.
If you change it to the following

   
aaaa
Copy after login

js is parsed after the span, the span will be there. In addition, the jquery method is to use The ready function contains these codes, it doesn’t matter where they are placed. Its function is to execute the included js only after the entire page is loaded, such as:

Copy after login

1,
Therefore, the setting and acquisition of span are as follows:

    

aaaa

Copy after login

2,

Ifwant to get the html code, just replace the text with html,

    

aaaa

Copy after login


##Set html and get html, as follows

#

    

初期值

Copy after login
Result:


三、注意点:

    

aaaa

Copy after login

结果



此时 ,获取的html()为 【testSpan】,而不是【testSpan

The above is the detailed content of How to get the value of span in jquery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!