gpt4 book ai didi

javascript - 从链接上方的跨度中获取文本 - Jquery

转载 作者:行者123 更新时间:2023-11-29 18:31:05 25 4
gpt4 key购买 nike

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

最佳答案

closest()走祖先树。由于您的 <span>元素是 anchor 的 sibling ,而不是祖先,你应该使用 prev()相反:

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

关于javascript - 从链接上方的跨度中获取文本 - Jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8225082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com