Jquery - 在特定的div中选择一个span的内容 [英] Jquery - Select a span's content inside of a specific div
问题描述
我试图选择一个div内的内容,但是我使用的CMS在我的div内增加了这个span,所以现在我不知道如何选择它里面的内容。该跨度在整个页面中使用了几次。所以我需要专门选择这个div内的一个。
这是HTML。
< div id =emailAddress>< span merge-tag ={{user.email}}>内容是什么变量应该设置为< / span>< ; / DIV>
我想将它设置为变量emailAddress。所以这里是完整的JQuery。基本上我只是基于我的CMS放置在div和span上面的内容显示/隐藏内容。
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();
}
});
执行此操作:
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}}>内容是什么变量应该设置TO< / span>< / 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>
这篇关于Jquery - 在特定的div中选择一个span的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!