如何克隆、更新和更新?使用 JavaScript 附加变量
我正在尝试创建一个具有“.comments”内值的变量,然后在“4 条评论”前面添加“阅读全部”一词。最后,将新变量附加到“#comments”。
设法编写一些内容,但它正在移动现有对象,而不是使用现有对象的值创建一个新对象。
下面是一些代码
<script type="text/javascript">
var commentsLink = document.querySelector('.story .comments a'),
commentsSubmit = document.querySelector('#comments .form-item-submit');
commentsLink.innerHTML = 'Read all ' + commentsLink.innerHTML;
commentsLink.className += 'readComments';
commentsSubmit.appendChild(commentsLink);
</script>
<div class="story">
<div class="comments">
<a href="http://foo.com">4 comments</a>
</div>
</div>
<div id="comments">
<div class="form-item-submit">Submit</div>
</div>
期望的结果:
<div class="story">
<div class="comments">
<a href="http://foo.com">4 comments</a>
</div>
</div>
<div id="comments">
<div class="form-item-submit">Submit</div>
<a href="http://foo.com" class="readComments">Read all 4 comments</a>
</div>
任何人都可以阐明这一点吗?请不要使用 Jquery。
I'm trying to create a variable with the value within '.comments' then prepend the word 'Read all' in front of '4 comments'. Finally, append the new variable to '#comments'.
Manage to write something but it's moving the existing object instead of creating a new object with the value of the existing one.
Some code below
<script type="text/javascript">
var commentsLink = document.querySelector('.story .comments a'),
commentsSubmit = document.querySelector('#comments .form-item-submit');
commentsLink.innerHTML = 'Read all ' + commentsLink.innerHTML;
commentsLink.className += 'readComments';
commentsSubmit.appendChild(commentsLink);
</script>
<div class="story">
<div class="comments">
<a href="http://foo.com">4 comments</a>
</div>
</div>
<div id="comments">
<div class="form-item-submit">Submit</div>
</div>
Desired result:
<div class="story">
<div class="comments">
<a href="http://foo.com">4 comments</a>
</div>
</div>
<div id="comments">
<div class="form-item-submit">Submit</div>
<a href="http://foo.com" class="readComments">Read all 4 comments</a>
</div>
Can anyone shed some light on this? No Jquery please.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
cloneNode
。要复制文本内容,请使用cloneNode(true)
。div
的 parent 元素,而不是提交div
本身。http://jsfiddle.net/eDGwj/
cloneNode
. To copy the text contents as well, usecloneNode(true)
.div
, not inside the submitdiv
itself.http://jsfiddle.net/eDGwj/
像这样的东西:
Something like this:
我想这就是你想要的,如果不是请评论:
关键函数是cloneNode(),注意Id的
更多信息
希望这有帮助
I think this is what you want, if not please comment:
The key function was cloneNode(), watch out with the Id's
More Info
Hope this helps