当使用数字动态创建 id 时如何访问 DOM 元素
我正在构建一个论坛页面,该页面针对一个与我们所在的主题非常相似的主题提交了许多评论。因此,当我访问数据库来显示评论时,我需要动态创建我的内容的某些 div
的 id,如下所示:
while($row=mysql_fetch_assoc($result))
{
?>
<div id="post<?=$row["postId"] ?>" class="seeForumPost"><?=$row["post"] ?> </div> //this one
<div id="postFooter<?=$row["postId"] ?>" class="seeForumPostFooter"> //this one
<div class="byUser">
by <a class="commonLink" href="<?php print "seeUser.php?userId=".$row['userId'] ?>"> <strong><?=ucwords($row["firstname"]) ?> <?=ucwords($row["lastname"]) ?></strong></a>
</div>
<div class="likeThisForum"> Like this Post</div>
<div class="numberOfLikes"> (<?=$row["postLikes"] ?> likes) </div>
<div id="<?=$row["postId"] ?>" onclick="reportPostSpam()" class="markAsSpam">Mark as spam</div> //this one
</div>
<?php } ?>
没错。我在 ID 中使用数字,因此它们看起来像 "post26"
"postFooter26"
和 "26"
但我不知道如何在我的ajax请求(原型)中获取这些DOM对象。我似乎无法处理那些 parentNode
和 previousSiblings
属性,因为 JavaScript 方法会抛出 cannot call property style of undefined
。感谢帮助。
I am building a forum page that submits a number of comments
with respect to a subject
much like the one we are on. Hence, when I acess the database to display the comments, I need to dynamically create the id of some div
's of my content, something like this:
while($row=mysql_fetch_assoc($result))
{
?>
<div id="post<?=$row["postId"] ?>" class="seeForumPost"><?=$row["post"] ?> </div> //this one
<div id="postFooter<?=$row["postId"] ?>" class="seeForumPostFooter"> //this one
<div class="byUser">
by <a class="commonLink" href="<?php print "seeUser.php?userId=".$row['userId'] ?>"> <strong><?=ucwords($row["firstname"]) ?> <?=ucwords($row["lastname"]) ?></strong></a>
</div>
<div class="likeThisForum"> Like this Post</div>
<div class="numberOfLikes"> (<?=$row["postLikes"] ?> likes) </div>
<div id="<?=$row["postId"] ?>" onclick="reportPostSpam()" class="markAsSpam">Mark as spam</div> //this one
</div>
<?php } ?>
Thats right. I am using numbers inside my id, so they appears like "post26"
"postFooter26"
and "26"
But I don't know how to fetch these DOM objects in my ajax request (prototype). I cant't seem to deal with those parentNode
and previousSiblings
properties because then the javascript method throws cannot call property style of undefined
. Helps are appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你总是可以尝试:
会更简单
You can always try:
Would be much simplier