从上面的链接获取文本 - Jquery

时间:2011-11-22 10:09:36

标签: javascript jquery text element closest

您好我正在尝试从链接上方的span元素中获取文本。

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

1 个答案:

答案 0 :(得分:4)

closest()走在祖先的树上。由于您的<span>元素是锚点的兄弟,而不是祖先,因此您应该使用prev()代替:

var widTitle = $(this).prev("span").text();