【发布时间】:2012-11-24 02:28:58
【问题描述】:
网页的结构是这样的:-
<div id='abc'>
<div class='a'>Some contents here </div>
<div class='b'>Some other contents< </div>
</div>
我的目标是在上述结构中的 a 类之后添加它。
<div class='a'>Some other contents here </div>
所以最终的结构看起来像这样:-
<div id='abc'>
<div class='a'>Some contents here </div>
<div class='a'>Some other contents here </div>
<div class='b'>Some other contents< </div>
</div>
有没有更好的方法可以使用 DOM 属性来做到这一点。我正在考虑解析内容和更新的幼稚方式。
如果我的疑问不清楚,请发表评论!
【问题讨论】:
-
唯一好的答案:使用 jquery
-
到目前为止您尝试过什么?您是否研究过如何向 DOM 添加元素?
-
@trebuchet:这太愚蠢了。
-
我试图使用 getELementById 获取 abc div 的内容。然后我正在解析内容以在正确的位置添加值
-
是的,DOM 不是字符串。它是节点的树形结构。通过 id 获取父元素后,您需要创建一个新元素并将其相对于其中一个子元素插入。
标签: javascript dom