您的代码中似乎有两个错误,如注释中所示(并粘贴在下面)。
您将脚本标记连接到comment\\u字段数组值中,而没有将其封装为字符串(引号中)。理想情况下,限制器函数的声明应该位于comments_args
正在传递给的数组comment_form
作用或者,可以通过将jquery计数器库添加到主题的函数中,将其加载到标记中。php(使用wp_enqueue_script)此外,您在错误的元素ID上初始化了“Limitor”函数"text" 而不是"comment" 在“注释字段”数组键中设置。我建议在文档完全加载/准备就绪后调用它为了便于参考,我复制并粘贴了您在评论中共享的代码(重新格式化以提高可读性),并发布了可能需要的编辑:
共享的实现(https://ideone.com/yFIXG3):
<div id="commentform" class="c-box">
<?php global $aria_req; $comments_args = array(
\'title_reply\'=>\'<h4><span>\'.__(\'Add Comment\', \'MyTheme\' ).\'</span></h4>\',
\'comment_notes_before\' => \'\',
\'comment_notes_after\' => \'\',
\'label_submit\' => __( \'Post Comment\', \'MyTheme\' ),
\'comment_field\' => \'<p class="comment-form-comment"><textarea id="comment" name="comment" cols="45" rows="8" aria-required="true" maxlength="600" placeholder="\'.__(\'Comment Description *\', \'MyTheme\' ).\'"></textarea>\'.<script>
(function ($) {
$.fn.extend({
limiter: function (minLimit, maxLimit, elem) {
$(this).on("keydown keyup focus keypress", function (e) {
setCount(this, elem, e);
});
function setCount(src, elem, e) {
var chars = src.value.length;
if (chars == maxLimit) {
//e.preventDefault();
elem.html(maxLimit - chars);
elem.addClass("maxLimit");
return false;
} else if (chars > maxLimit) {
src.value = src.value.substr(0, maxLimit);
chars = maxLimit;
elem.addClass("maxLimit");
} else {
elem.removeClass("maxLimit");
}
if (chars < minLimit) {
elem.addClass("minLimit");
} else {
elem.removeClass("minLimit");
}
elem.html(maxLimit - chars);
}
setCount($(this)[0], elem);
}
});
})(jQuery);
var elem = $("#chars");
$("#text").limiter(35, 200, elem);
</script>.\'</p>\',
\'fields\' => apply_filters( \'comment_form_default_fields\',
array(
\'author\' => \'<p class="comment-form-author">\'
.( $req ? \'\' : \'\' ).\'<input id="author" name="author" type="text" aria-required="true" maxlength="20" placeholder="\'.__(\'Name *\', \'MyTheme\' ).\'" value="\'.esc_attr( $commenter[\'comment_author\'] ) . \'" size="30"\' . $aria_req . \' /></p>\',
\'email\' => \'<p class="comment-form-email">\'
.($req ? \'\' : \'\' ) . \'<input id="email" name="email" type="text" aria-required="true" maxlength="50" placeholder="\'.__(\'Email *\', \'MyTheme\' ).\'" value="\' . esc_attr( $commenter[\'comment_author_email\'] ).\'" size="30"\'.$aria_req.\' /></p>\',
\'url\' => \'<p class="comment-form-url"><input id="url" name="url" type="text" placeholder="\'.__(\'Website\', \'MyTheme\' ).\'" value="\' . esc_url( $commenter[\'comment_author_url\'] ) . \'" size="30" /></p>\'
)
)
);
comment_form($comments_args); ?>
</div>
您的实现已共享,但已格式化:
<div class="c-box" id="commentform">
<?php
global $aria_req;
$comments_args = array(
\'title_reply\' =>
\'<h4><span>\'.__(\'Add Comment\', \'MyTheme\' ).\'</span></h4>\',
\'comment_notes_before\' =>
\'\',
\'comment_notes_after\' =>
\'\',
\'label_submit\' =>
__( \'Post Comment\', \'MyTheme\' ),
\'comment_field\' =>
\'<p class="comment-form-comment"><textarea id="comment" name="comment" cols="45" rows="8" aria-required="true" maxlength="600" placeholder="\'.__(\'Comment Description *\', \'MyTheme\' ).\'"></textarea>\'.
<script>
(function($) {
$.fn.extend({
limiter: function(minLimit, maxLimit, elem) {
$(this).on("keydown keyup focus keypress", function(e) {
setCount(this, elem, e);
});
function setCount(src, elem, e) {
var chars = src.value.length;
if (chars == maxLimit) {
//e.preventDefault();
elem.html(maxLimit - chars);
elem.addClass("maxLimit");
return false;
} else if (chars > maxLimit) {
src.value = src.value.substr(0, maxLimit);
chars = maxLimit;
elem.addClass("maxLimit");
} else {
elem.removeClass("maxLimit");
}
if (chars < minLimit) {
elem.addClass("minLimit");
} else {
elem.removeClass("minLimit");
}
elem.html(maxLimit - chars);
}
setCount($(this)[0], elem);
}
});
})(jQuery);
var elem = $("#chars");
$("#text").limiter(35, 200, elem);
</script>
.\'</p>\',
\'fields\' =>
apply_filters( \'comment_form_default_fields\',
array(
\'author\' =>
\'<p class="comment-form-author">\'.( $req ? \'\' : \'\' ).\'<input id="author" name="author" type="text" aria-required="true" maxlength="20" placeholder="\'.__(\'Name *\', \'MyTheme\' ).\'" value="\'.esc_attr( $commenter[\'comment_author\'] ) . \'" size="30"\' . $aria_req . \' /></p>\',
\'email\' =>
\'<p class="comment-form-email">\'.($req ? \'\' : \'\' ) . \'<input id="email" name="email" type="text" aria-required="true" maxlength="50" placeholder="\'.__(\'Email *\', \'MyTheme\' ).\'" value="\' . esc_attr( $commenter[\'comment_author_email\'] ).\'" size="30"\'.$aria_req.\' /></p>\',
\'url\' =>
\'<p class="comment-form-url"><input id="url" name="url" type="text" placeholder="\'.__(\'Website\', \'MyTheme\' ).\'" value="\' . esc_url( $commenter[\'comment_author_url\'] ) . \'" size="30" /></p>\'
)
)
);
comment_form($comments_args);
?>
</div>
建议调整:
<div class="c-box" id="commentform">
<?php
global $aria_req;
$comments_args = array(
\'title_reply\' =>
\'<h4><span>\'.__(\'Add Comment\', \'MyTheme\' ).\'</span></h4>\',
\'comment_notes_before\' =>
\'\',
\'comment_notes_after\' =>
\'\',
\'label_submit\' =>
__( \'Post Comment\', \'MyTheme\' ),
\'comment_field\' =>
\'<p class="comment-form-comment"><textarea id="comment" name="comment" cols="45" rows="8" aria-required="true" maxlength="600" placeholder="\'.__(\'Comment Description *\', \'MyTheme\' ).\'"></textarea></p>\',
\'fields\' =>
apply_filters( \'comment_form_default_fields\',
array(
\'author\' =>
\'<p class="comment-form-author">\'.( $req ? \'\' : \'\' ).\'<input id="author" name="author" type="text" aria-required="true" maxlength="20" placeholder="\'.__(\'Name *\', \'MyTheme\' ).\'" value="\'.esc_attr( $commenter[\'comment_author\'] ) . \'" size="30"\' . $aria_req . \' /></p>\',
\'email\' =>
\'<p class="comment-form-email">\'.($req ? \'\' : \'\' ) . \'<input id="email" name="email" type="text" aria-required="true" maxlength="50" placeholder="\'.__(\'Email *\', \'MyTheme\' ).\'" value="\' . esc_attr( $commenter[\'comment_author_email\'] ).\'" size="30"\'.$aria_req.\' /></p>\',
\'url\' =>
\'<p class="comment-form-url"><input id="url" name="url" type="text" placeholder="\'.__(\'Website\', \'MyTheme\' ).\'" value="\' . esc_url( $commenter[\'comment_author_url\'] ) . \'" size="30" /></p>\'
)
)
);
comment_form($comments_args);
?>
<script>
(function($) {
$.fn.extend({
limiter: function(minLimit, maxLimit, elem) {
$(this).on("keydown keyup focus keypress", function(e) {
setCount(this, elem, e);
});
function setCount(src, elem, e) {
var chars = src.value.length;
if (chars == maxLimit) {
//e.preventDefault();
elem.html(maxLimit - chars);
elem.addClass("maxLimit");
return false;
} else if (chars > maxLimit) {
src.value = src.value.substr(0, maxLimit);
chars = maxLimit;
elem.addClass("maxLimit");
} else {
elem.removeClass("maxLimit");
}
if (chars < minLimit) {
elem.addClass("minLimit");
} else {
elem.removeClass("minLimit");
}
elem.html(maxLimit - chars);
}
setCount($(this)[0], elem);
}
});
$(document).ready(function() {
var elem = $("#chars");
$("#comment").limiter(35, 200, elem);
});
})(jQuery);
</script>
</div><!--/#commentform-->