JQuery 获取父级中的类
我确实花了几个小时使用 JQuery 尝试从类中提取一段文本,在下面的代码中您可以看到,当单击 reply 按钮时,JQuery 会查找名为 name< 的类。 /strong> 以提取一段文本,在本例中为 bob green。
我总是研究这类事情,但由于这些代码都不起作用,我陷入了困境,在下面的 JQuery 代码中,您可以看到我尝试了不同的方法来提取文本。
如果有人能帮助我解决这个问题,我将不胜感激。
<div class='replyContainer'>
<div class='image'></div>
<span>
<div class='close' title='Close'></div>
<b><font class='name'>bob green</font></b>
test message
</span>
<div class='bubble-triangle-border'></div>
<div class='bubble-triangle'></div>
<div class='info'>
<div class='reply'></div>
</div>
</div>
$(".replyContainer .reply").live('click',function(){
var replyName = "@" + $(this).parent().parent(".name").text();
});
$(".replyContainer .reply").live('click',function(){
var replyName = "@" + $(this).parent().parent().closest(".name").text();
});
$(".replyContainer .reply").live('click',function(){
var replyName = "@" + $(this).parent().parent("span font.name").text();
});
I have literally spent hours with JQuery trying to extract a piece of text from a class, in the code below you can see that when the reply button is clicked JQuery looks for a class called name in order to extract a piece of text which in this instance is bob green.
I am always one to research these type of things but since none of this code works I am stuck, in the JQuery code below you can see that I have tried different ways in extracting the text.
If anyone could help me fix this problem I would be more than grateful.
<div class='replyContainer'>
<div class='image'></div>
<span>
<div class='close' title='Close'></div>
<b><font class='name'>bob green</font></b>
test message
</span>
<div class='bubble-triangle-border'></div>
<div class='bubble-triangle'></div>
<div class='info'>
<div class='reply'></div>
</div>
</div>
$(".replyContainer .reply").live('click',function(){
var replyName = "@" + $(this).parent().parent(".name").text();
});
$(".replyContainer .reply").live('click',function(){
var replyName = "@" + $(this).parent().parent().closest(".name").text();
});
$(".replyContainer .reply").live('click',function(){
var replyName = "@" + $(this).parent().parent("span font.name").text();
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
其他一些事情:
.delegate()
或 .on()
代替。标签,而应该使用 CSS!
closest()
向上查找 DOM 父级及其父级,而find()
向下查找子级!A few other things:
.delegate()
or .on()
instead.<font>
tag but use CSS!closest()
goes up the DOM parents and their parents, andfind()
goes downward and looks at children!