[英]How do I retrieve text from a span tag inside a selected href tag?
I'm trying to retrieve the text from a span elementinside a selected href tag. 我正在尝试从span elementinside中检索选定的href标记中的文本。 Here is my HTML from a previous example:
这是我之前的例子中的HTML:
<ul class="thumbnails">
<li>
<a href="#" class="thumbnail">
<img class="giftthumb" src='thumb1.jpg' alt="">
<span class="gifttitle">Thumb1</span>
</a>
</li>
<li>
<a href="#" class="thumbnail">
<img class="giftthumb" src='thumb2.jpg' alt="">
<span class="gifttitle">Thumb2</span>
</a>
</li>
<li>
<a href="#" class="thumbnail">
<img class="giftthumb" src='thumb3.jpg' alt="">
<span class="gifttitle">Thumb3</span>
</a>
</li>
</ul>
<input type="button" class="btn btn-success" value="Test" id="capture"/>
My jQuery looks like this 我的jQuery看起来像这样
$('#capture').click(function(e) {
e.preventDefault();
var test = $(".highlight .gifttitle").text();
alert("TEXT " + test);
});
Once an item is selected I add a class called: 'highlight' 选择一个项目后,我添加一个名为“highlight”的类
你只需要一个简单的类选择器:
var test = $(".gifttitle.highlight").text();
You shold change your code to: 您将代码更改为:
$('#capture').click(function(e) {
e.preventDefault();
// var test = $(".gifttitle", this).text();
alert("TEXT " + test);
});
The :selected
pseudoclass is not applied to span
elements. :selected
伪类不适用于span
元素。
The $(". gifttitle ", this)
means select the element with class gifttitle
in current element. $(". gifttitle ", this)
表示在当前元素中选择具有类gifttitle
的元素。
var test = $('。highlight> .gifttitle')。text();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.