2

您好我正在尝试从链接上方的 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>
4

1 回答 1

4

最近的()遍历祖先树。由于您的<span>元素是锚的兄弟,而不是祖先,您应该使用prev()代替:

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