简体   繁体   English

使用 javascript 向 html 正文添加评论和回复按钮

[英]add comment and reply button to html body using javascript

I want to add html elements to the body of my page as an unordered list.我想将 html 元素作为无序列表添加到我的页面正文中。 I have used DocumentFragment method to create a fragment of the reply button and comment span .我使用 DocumentFragment 方法创建了reply buttoncomment span的片段。 Now I need to add a textbox and a add reply to that ul whenever a user clicks on the reply button and add all the replies as a list next to respective comment.现在,每当用户单击回复按钮并将所有回复添加为相应评论旁边的列表时,我都需要添加一个textbox和对该 ul 的add reply Here is what I've tried:这是我尝试过的:

 function comment() { var my_comment = document.getElementById('comments'); my_comment.innerHTML = "<textarea id='user_comment'> </textarea> <button onclick='addNewItem()'>Post Comment</button>"; } function addNewItem() { var thediv = document.getElementById("comments_and_replies"); var listItem = document.createElement("ul"); var replyBox = document.createElement("textbox"); var commentSpan = document.createElement("span"); var user_comment = document.getElementById('user_comment'); var replyButton = document.createElement("button"); listItem.className = "comments-list"; replyButton.innerText = "Reply"; replyButton.className = "reply"; replyButton.addEventListener("click", function() { var g = document.getElementById('comments_and_replies'); for (var i = 0, len = g.children.length; i < len; i++) { (function(index) { g.children[i].onclick = function() { listItem.insertBefore(replyBox, listItem.children[index]); } })(i); } }) commentSpan.textContent = user_comment.value; var documentFragment = document.createDocumentFragment(); documentFragment.appendChild(listItem); listItem.appendChild(commentSpan); listItem.appendChild(replyButton); thediv.appendChild(documentFragment); }
 <section><button onclick="comment()">Leave a comment</button></section> <div id="comments"></div> <div id="comments_and_replies"></div>

Event delegation on a single <form> can accommodate an unlimited amount of <button> s even if they are added after the page has loaded.单个<form>上的事件委托可以容纳无限数量的<button> ,即使它们是在页面加载后添加的。

The example below uses the following:下面的示例使用以下内容:

Note: Unless you are submitting data to a server, add type="button" to each <button>注意:除非您将数据提交到服务器,否则将type="button"添加到每个<button>

Details are commented in code below详细信息在下面的代码中注释

 // Refernce <form> const form = document.forms.commentsReplies; // Any click on <form> invokes post() form.onclick = post; // Pass the event function post(event) { /* Reference all <fieldset> (also <button>, <textarea>, etc) */ const field = event.currentTarget.elements; // Reference the actual element clicked const clicked = event.target; // if element clicked has class postCom if (clicked.matches('.postCom')) { /* find <fieldset name="post"> and insert HTML into it */ field.post.insertAdjacentHTML('beforeEnd', `<fieldset name='commentPost'><textarea></textarea><button class='comTxt' type='button'>Done</button></fieldset>`); // Otherwise if clicked element has class comTxt } else if (clicked.matches('.comTxt')) { /* find the clicked element's element that is right before it and get it's text */ const text = clicked.previousElementSibling.value; /* find <fieldset name='comments'> and insert HTML */ field.comments.insertAdjacentHTML('afterBegin', `<fieldset>${text}<button class='postRep' type='button'>Reply</button><ul></ul></fieldset>`); // Remove <fieldset name='commentPost'> field.commentPost.remove(); } else if (clicked.matches('.postRep')) { clicked.insertAdjacentHTML('afterEnd', `<ul><textarea></textarea><button class='repTxt' type='button'>Done</button></ul>`); } else if (clicked.matches('.repTxt')) { const text = clicked.previousElementSibling.value; const list = clicked.parentElement; list.insertAdjacentHTML('afterBegin', `<li>${text}<button class='postRep' type='button'>Reply</button></li>`); clicked.previousElementSibling.remove(); clicked.remove(); } else { return false; } }
 button { display: block; margin-left: 25%; }
 <form id='commentsReplies'> <fieldset name='post'><button class='postCom' type='button'>Leave a comment</button> </fieldset> <fieldset name="comments"> <legend>Comments</legend> </fieldset> </form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM