[英]How to add <span> tag to a selected text in JQuery
[英]How do I retrieve text from a span tag inside a selected href tag?
我正在嘗試從span elementinside中檢索選定的href標記中的文本。 這是我之前的例子中的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"/>
我的jQuery看起來像這樣
$('#capture').click(function(e) {
e.preventDefault();
var test = $(".highlight .gifttitle").text();
alert("TEXT " + test);
});
選擇一個項目后,我添加一個名為“highlight”的類
你只需要一個簡單的類選擇器:
var test = $(".gifttitle.highlight").text();
您將代碼更改為:
$('#capture').click(function(e) {
e.preventDefault();
// var test = $(".gifttitle", this).text();
alert("TEXT " + test);
});
:selected
偽類不適用於span
元素。
$(". gifttitle ", this)
表示在當前元素中選擇具有類gifttitle
的元素。
var test = $('。highlight> .gifttitle')。text();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.