gpt4 book ai didi

jquery - 如何在jquery中通过attr()获取text()

转载 作者:行者123 更新时间:2023-12-01 00:57:48 25 4
gpt4 key购买 nike

问题来了

<div class="question">
<div class="button" data-q="1" data-a="1"><a href="javascript:void();">2002</a></div>
<div class="button" data-q="1" data-a="2" data-g="true"><a href="javascript:void();">2010</a></div>
<div class="button" data-q="1" data-a="3"><a href="javascript:void();">1985</a></div>
<div class="button" data-q="1" data-a="4"><a href="javascript:void();">2013</a></div>
</div>

我想要设置有 data-g 的 attr() 的 text() 值。

现在我尝试的代码不起作用

$('.question .button a').click(function(){    
var aText = $(this).parents('.question').find('data-g').text();
return aText;
});

我已经尝试过这个,但我得到了 .question DIV 中的所有文本(2002,2010,1985,2013)

$('.question .button a').click(function(){    
var aText = $(this).parents('.question').text();
return aText;
});

那么我怎样才能只得到带有 data-g 属性的 DIV 的结果,并只得到 2010 作为文本。

谢谢

最佳答案

$('.question .button a').click(function() {
var aText = $(this).closest(".question").find("[data-g]").text();
// to check for `true` use ("[data-g='true']")
console.log(aText);
});​

演示: http://jsfiddle.net/2ny7j/

关于jquery - 如何在jquery中通过attr()获取text(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10882842/

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