fix: 回复按钮用 href=#respond + onclick 聚焦输入框

This commit is contained in:
2026-05-17 13:13:32 +08:00
parent 59c52a8bd6
commit 19f75da803
2 changed files with 43 additions and 48 deletions
+42 -47
View File
@@ -27,7 +27,7 @@ if (post_password_required()) return;
</div>
<div class="comment-item__text"><?php echo esc_html($c->comment_content); ?></div>
<div class="comment-item__reply">
<a href="javascript:void(0)" onclick="replyTo(<?php echo $c->comment_ID; ?>)">回复</a>
<a href="#respond" onclick="document.getElementById('comment_parent').value='<?php echo $c->comment_ID; ?>';document.getElementById('comment').focus()">回复</a>
</div>
</li>
<?php endforeach; ?>
@@ -35,53 +35,48 @@ if (post_password_required()) return;
<?php endif; ?>
<?php if (is_user_logged_in()) : ?>
<?php comment_form(array(
'title_reply' => '',
'comment_notes_before' => '',
'comment_notes_after' => '',
'logged_in_as' => '',
'fields' => array(),
'comment_field' => '<textarea id="comment" name="comment" rows="3" placeholder="写点什么..." required></textarea>',
'submit_button' => '<button type="submit" class="submit">落笔留痕</button>',
'submit_field' => '<div class="form-submit">%1$s %2$s</div>',
'label_submit' => '落笔留痕',
)); ?>
<?php else : ?>
<form id="commentform-guest" class="comment-form" novalidate>
<textarea id="comment" name="comment" rows="3" placeholder="写点什么..." required></textarea>
<input type="hidden" id="comment_parent" name="comment_parent" value="0">
<div class="form-submit">
<button type="submit" class="submit">落笔留痕</button>
</div>
</form>
<?php endif; ?>
<?php if (!is_user_logged_in()) : ?>
<div id="login-modal" class="login-modal" style="display:none">
<div class="login-modal__content">
<p>登录后才能留言</p>
<button onclick="goLogin()" class="submit">去登录</button>
<button onclick="closeLoginModal()" class="cancel-btn">取消</button>
<div id="respond" class="comment-respond">
<form id="commentform" class="comment-form" method="post" action="<?php echo site_url('/wp-comments-post.php'); ?>">
<textarea id="comment" name="comment" rows="3" placeholder="写点什么..." required></textarea>
<input type="hidden" name="comment_post_ID" value="<?php echo get_the_ID(); ?>">
<input type="hidden" id="comment_parent" name="comment_parent" value="0">
<?php wp_nonce_field('comment_nonce_' . get_the_ID(), 'comment_nonce'); ?>
<div class="form-submit">
<button type="submit" class="submit">落笔留痕</button>
</div>
</form>
</div>
</div>
<script>
function replyTo(cid) {
document.getElementById('comment_parent').value = cid;
document.getElementById('comment').focus();
}
function goLogin() {
window.location.href = '/wp-login.php?redirect_to=' + encodeURIComponent(window.location.href);
}
function closeLoginModal() {
var m = document.getElementById('login-modal');
if (m) m.style.display = 'none';
}
document.getElementById('commentform-guest').addEventListener('submit', function(e) {
e.preventDefault();
var m = document.getElementById('login-modal');
if (m) m.style.display = 'flex';
});
</script>
<?php else : ?>
<div id="respond" class="comment-respond">
<form id="commentform-guest" class="comment-form" novalidate>
<textarea id="comment" name="comment" rows="3" placeholder="登录后写点什么..." required></textarea>
<input type="hidden" id="comment_parent" name="comment_parent" value="0">
<div class="form-submit">
<button type="submit" class="submit">落笔留痕</button>
</div>
</form>
</div>
<div id="login-modal" class="login-modal">
<div class="login-modal__content">
<p>登录后才能留言</p>
<button onclick="goLogin()" class="submit">去登录</button>
<button onclick="closeLoginModal()" class="cancel-btn">取消</button>
</div>
</div>
<script>
document.getElementById('commentform-guest').addEventListener('submit', function(e) {
e.preventDefault();
var m = document.getElementById('login-modal');
if (m) m.style.display = 'flex';
});
function closeLoginModal() {
var m = document.getElementById('login-modal');
if (m) m.style.display = 'none';
}
function goLogin() {
window.location.href = '/wp-login.php?redirect_to=' + encodeURIComponent(window.location.href);
}
</script>
<?php endif; ?>
</div>