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

War*_*ace 3 jquery

这是问题所在

<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>
Run Code Online (Sandbox Code Playgroud)

我想为attr()设置data-g的text()值.

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

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

我试过这个,但我得到了.question DIV(2002,2010,1985,2013)内的所有文字

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

那么我怎样才能只使用带有data-g属性的DIV结果并仅将2010作为文本.

谢谢

Vis*_*ioN 5

$('.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);
});?
Run Code Online (Sandbox Code Playgroud)

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