我有 DIV 及其子 DIV ,我想使用 jQuery 选择最后一个 DIV 。父级 DIVS 有 id
这是问题的描述。我有父 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如前所述,ID 必须是唯一的。话虽这么说,这里有一种方法可以完成您的要求(在适当的情况下使用类):
HTML w/classs(仅供参考):
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):
HTML w/ classes (for reference):
jsFiddle: http://jsfiddle.net/yT8Fc/
这不起作用,因为它给出了大于比较的结果作为参数。它必须看起来像这样:
This does not work as it gives the result of the bigger than comparison as parameter. It has to look like this: