我有 DIV 及其子 DIV ,我想使用 jQuery 选择最后一个 DIV 。父级 DIVS 有 id

发布于 2024-12-29 08:37:31 字数 1334 浏览 0 评论 0原文

这是问题的描述。我有父 div(allcomments_4) ,而不是在它里面,我有几个子 div(oneEntry) ,里面有自己的子节点,但我只需要找到父节点 (allComments_4) 的最后一个子节点。我想读取 div(lastComment) 内部的内容,而不是将其添加到此 div 上方,这与使用 jquery 的上面的 div (oneEntry) 类似。我想使用 ids 而不是类来进行此操作。当我计算 allComments_4 div 的 id 时。我这样尝试过。

<script>
    var allcommentsId =allcomments_4
    var right = "div#lastComment"
    var left = '#'+allcommentsId 
    var message = $(left>right ).html()
    alert(message)
</script>

<div id="allcomments_4">
 <div class="oneEntry">
   <div class="commentImage">
   </div>
   <div class="commentEntry">
   <div class="commentText">
   </div>
   <div class="commentDate">
    Commented On -"Thu Jan 26 16:44:16 EST 2012"/>
   </div>
 </div>
 <div class="oneEntry">
   <div class="commentImage">
   </div>
   <div class="commentEntry">
   <div class="commentText">
   </div>
   <div class="commentDate">
    Commented On -"Thu Jan 26 16:44:16 EST 2012"/>
   </div>
 </div>
</div><!-- One Comment Entry Ends here -->
<div id ="lastComment">
</div><!-- last Comment ends here -->
</div><!-- All Comments Ends here -->

我觉得遗漏了一些东西,我很抱歉问了这么蹩脚的问题,因为我是 jquery 和整个 javascript 的初学者。

Here is the description of problem . I have parent div(allcomments_4) and than inside it , i have few child divs(oneEntry) with their own children inside but i just need to find the last child of the parent node(allComments_4). I want to read the content inside of div(lastComment) it than add it above this div which will be similar like above div (oneEntry) using jquery. I want to do this manipulation using ids not class. As i calculate the id of allComments_4 div . I tried like this.

<script>
    var allcommentsId =allcomments_4
    var right = "div#lastComment"
    var left = '#'+allcommentsId 
    var message = $(left>right ).html()
    alert(message)
</script>

<div id="allcomments_4">
 <div class="oneEntry">
   <div class="commentImage">
   </div>
   <div class="commentEntry">
   <div class="commentText">
   </div>
   <div class="commentDate">
    Commented On -"Thu Jan 26 16:44:16 EST 2012"/>
   </div>
 </div>
 <div class="oneEntry">
   <div class="commentImage">
   </div>
   <div class="commentEntry">
   <div class="commentText">
   </div>
   <div class="commentDate">
    Commented On -"Thu Jan 26 16:44:16 EST 2012"/>
   </div>
 </div>
</div><!-- One Comment Entry Ends here -->
<div id ="lastComment">
</div><!-- last Comment ends here -->
</div><!-- All Comments Ends here -->

I think missing something and i am sorry for asking such lame question as i am beginner in jquery and this whole javascript thing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

关于从前 2025-01-05 08:37:31

如前所述,ID 必须是唯一的。话虽这么说,这里有一种方法可以完成您的要求(在适当的情况下使用类):

function displayContent(IdNum) {
    var Id = '#allcomments_' + IdNum;
    var $lastComment = $(Id).find('.lastComment');
    var message = $lastComment.html();
    alert(message);
}

displayContent(4);

HTML w/classs(仅供参考):

<div id="allcomments_4">
    <div class="oneEntry">
        <div class="commentImage">
        </div>
        <div class="commentEntry">
            <div class="commentText">
            </div>
            <div class="commentDate">
            Commented On - "Thu Jan 26 16:44:16 EST 2012"
            </div>
        </div>
    </div>
    <div class="oneEntry">
        <div class="commentImage">
        </div>
        <div class="commentEntry">
            <div class="commentText">
            </div>
            <div class="commentDate">
            Commented On - "Thu Jan 26 16:44:16 EST 2012"
            </div>
        </div>
    </div><!-- One Comment Entry Ends here -->
    <div class="lastComment">
        Body of lastComment
    </div><!-- last Comment ends here -->
</div><!-- All Comments Ends here -->

jsFiddle: http://jsfiddle.net/yT8Fc/

As mentioned, IDs must be unique. That being said, here is a way to accomplish what you asked (using classes where appropriate):

function displayContent(IdNum) {
    var Id = '#allcomments_' + IdNum;
    var $lastComment = $(Id).find('.lastComment');
    var message = $lastComment.html();
    alert(message);
}

displayContent(4);

HTML w/ classes (for reference):

<div id="allcomments_4">
    <div class="oneEntry">
        <div class="commentImage">
        </div>
        <div class="commentEntry">
            <div class="commentText">
            </div>
            <div class="commentDate">
            Commented On - "Thu Jan 26 16:44:16 EST 2012"
            </div>
        </div>
    </div>
    <div class="oneEntry">
        <div class="commentImage">
        </div>
        <div class="commentEntry">
            <div class="commentText">
            </div>
            <div class="commentDate">
            Commented On - "Thu Jan 26 16:44:16 EST 2012"
            </div>
        </div>
    </div><!-- One Comment Entry Ends here -->
    <div class="lastComment">
        Body of lastComment
    </div><!-- last Comment ends here -->
</div><!-- All Comments Ends here -->

jsFiddle: http://jsfiddle.net/yT8Fc/

弥繁 2025-01-05 08:37:31
var message = $(left>right ).html()

这不起作用,因为它给出了大于比较的结果作为参数。它必须看起来像这样:

var message = $(left + ' > ' + right).html()
var message = $(left>right ).html()

This does not work as it gives the result of the bigger than comparison as parameter. It has to look like this:

var message = $(left + ' > ' + right).html()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文