简体   繁体   中英

Jquery - Select a span's content inside of a specific div

I am trying to select the content inside of a div but the CMS I am using adds this span inside of my div and so now I am not sure how to select the content inside of it. That span is used a couple times throughout the page. So I need to specifically select the one inside of this div.

This is the HTML.

<div id="emailAddress"><span merge-tag="{{user.email}}">CONTENT HERE IS WHAT THE VARIABLE SHOULD BE SET TO</span></div>

And I want to set it as the variable "emailAddress". So here is the full JQuery. Basically I am just showing/hiding content based on what my CMS puts in the div and span above.

var emailAddress = ????????????;

 $(document).ready(function() {

    if (emailAddress == 'email1@email.com') {
        $('#section1').show();
    } 

    else if (emailAddress == 'email2@email.com') {
        $('#section2').show();
    } 

    else if (emailAddress == 'email3@email.com') {
        $('#section3').show();
    } 

    else {
        $('#default-content').show();
    }
});

Do this:

 var emailAddress = null; $(document).ready(function() { emailAddress = $('#emailAddress span').text(); console.log(emailAddress); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="emailAddress"><span merge-tag="{{user.email}}">CONTENT HERE IS WHAT THE VARIABLE SHOULD BE SET TO</span></div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM