2

こんにちは私はリンクのすぐ上のスパン要素からテキストを取得しようとしています。

$(this).closest("span").text()

完全なコード:

JS

$boxes.find('.portlet-header .portlet-maximize').click(function() {
// returns the rel of the link fine   
var widHtml = $(this).attr('rel'); 
// get text from span above link (not working)
var widTitle = $(this).closest("span").text()
});

HTML

<div class="portlet-header">
<span class="widTitle gmIcon">text I want to get</span>
<a rel="widgets/dashboard/max/goal-mouth" class="portlet-maximize"></a>
</div>
4

1 に答える 1

4

closest()は祖先ツリーをたどります。<span>要素は祖先ではなくアンカーの兄弟であるため、代わりに prev() を使用する必要があります

var widTitle = $(this).prev("span").text();
于 2011-11-22T10:12:52.200 に答える