这是我试图使用的选择器,以便返回它下面的数据。
$('.groupPopper').click(function(event){
var name = $(this).index(".name").contents();
alert(name);
});
<a class="groupPopper">
[text class="name">Name here</a>
</a>
如何通过点击jquery中的元素'a'来返回它的类的文本元素?我的例子不起作用..
答案 0 :(得分:1)
假设你在jQuery下发布的内容应该是HTML,这应该可行:
$('.groupPopper').click(function(event){
var name = $(".name", this).text();
alert(name);
});
答案 1 :(得分:0)
试试这个。 jsfiddle
$('.groupPopper').click(function(event){
var name = $(".name", this).text();
alert(name);
});
答案 2 :(得分:0)
try using `.text()`
$('.groupPopper').click(function(event){
var name = $(this).find('.name').text();
alert(name);
});
答案 3 :(得分:0)
要按类名返回子项,可以在jQuery中使用.find
:
$('.groupPopper').click(function(event){
var name = $(this).find(".name").text();
alert(name);
});
或者如果您有多个此类的孩子,您可以使用.each
进行迭代:
$('.groupPopper').click(function(event){
$(this).find(".name").each(function() {;
var name = $(this).text();
alert(name);
});
});