简体   繁体   中英

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. Here is my HTML from a previous example:

<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

$('#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'

你只需要一个简单的类选择器:

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.

The $(". gifttitle ", this) means select the element with class gifttitle in current element.

var test = $('。highlight> .gifttitle')。text();

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