洛丽糖(luolt.cn),致力于互联网资源的共享, 分享各类技术教程,typecho主题模板,zblog主题模板,网站源码等各种资源。
首页 » 收集 » 正文内容
html评论列表回复表单定位
寻梦xunm| 85| 收集
9天前

进行更新微博客程序的时候,准备重新修改一下评论插件的回复表单定位问题,经过AI反复修改生成最终代码如下。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Comment List with Reply Form</title>
    <style>
        #comment-section {
            margin-bottom: 20px;
        }

        .comment {
            margin-bottom: 15px;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }

        .reply-form {
            margin-top: 10px;
        }

        .reply-form textarea {
            width: 100%;
            height: 60px;
            margin-bottom: 10px;
        }

        #bottom-reply-form {
            margin-top: 20px;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }

        #bottom-reply-form textarea {
            width: 100%;
            height: 60px;
            margin-bottom: 10px;
        }
    </style>
</head>
<body>
    <div id="comment-section">
        <div class="comment">
            <p>This is a comment.</p>
            <button class="reply-btn">回复</button>
        </div>
        <div class="comment">
            <p>This is another comment.</p>
            <button class="reply-btn">回复</button>
        </div>
    </div>
    <form id="bottom-reply-form">
        <textarea placeholder="Write your reply..."></textarea>
        <button type="submit">Submit</button>
    </form>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const replyButtons = document.querySelectorAll('.reply-btn');
            const replyForm = document.getElementById('bottom-reply-form');
            let originalParent = replyForm.parentElement; // Store the original parent of the reply form

            replyButtons.forEach(button => {
                button.addEventListener('click', function() {
                    if (this.textContent === '取消') {
                        // If the button text is "Cancel", move the form back to the bottom
                        originalParent.appendChild(replyForm);
                        this.textContent = '回复';
                    } else {
                        // Otherwise, move the form to the clicked comment
                        replyButtons.forEach(btn => {
                            if (btn !== this) {
                                // Reset other buttons to "Reply"
                                btn.textContent = '回复';
                                if (btn.parentElement.classList.contains('comment') && btn.nextElementSibling === replyForm) {
                                    originalParent.appendChild(replyForm);
                                }
                            }
                        });

                        // Move the form to the clicked comment
                        if (replyForm.parentElement.classList.contains('comment')) {
                            // If the form is already in a comment, move it back to the bottom
                            originalParent.appendChild(replyForm);
                        }
                        this.parentNode.appendChild(replyForm);
                        this.textContent = '取消';
                    }
                });
            });
        });
    </script>
</body>
</html>
none
0 赞 or 打赏
喜欢就打赏一点
微信 支付宝
20240430140454171445709417079.png
20240430140454171445709417079.png
隐私
Q Q:1340326824
邮箱:vipshiyi@qq.com
QQ群:422720328

我的音乐