JQuery 获取父级中的类

发布于 2024-12-25 02:57:30 字数 1203 浏览 3 评论 0原文

我确实花了几个小时使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

失眠症患者 2025-01-01 02:57:30
 $(".replyContainer .reply").live("click", function() { 
      var replyName = "@" + $(this).closest(".replyContainer").find(".name").text()
 })

其他一些事情:

  1. jQuery.live() 被认为已弃用(请参阅: http://api.jquery.com/ live/),您应该使用 .delegate() 或 .on() 代替。
  2. 你完全不应该使用 标签,而应该使用 CSS!
  3. closest() 向上查找 DOM 父级及其父级,而 find() 向下查找子级!
 $(".replyContainer .reply").live("click", function() { 
      var replyName = "@" + $(this).closest(".replyContainer").find(".name").text()
 })

A few other things:

  1. jQuery.live() is considered deprecated (see: http://api.jquery.com/live/), you should use .delegate() or .on() instead.
  2. You should totally not use the <font> tag but use CSS!
  3. closest() goes up the DOM parents and their parents, and find() goes downward and looks at children!
爱格式化 2025-01-01 02:57:30
$(".replyContainer .reply").live('click',function(){    
var replyName = "@" + $(this).closest('div.replyContainer').find('font.name').text();
});
$(".replyContainer .reply").live('click',function(){    
var replyName = "@" + $(this).closest('div.replyContainer').find('font.name').text();
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文