我的解决方案(与 WordPress TinyMCE 设置有关):
jQuery( function ( $ ) {
$( '.smyles-add-required-attr' ).attr( 'required', 'required' );
tinymce.on( 'AddEditor', function ( e ) {
console.log( 'Added editor with id: ' + e.editor.id, e.editor );
var $editor = tinymce.get( e.editor.id );
$editor.on( "change", function (e){
$editor.save();
});
});
} );
上面的 jQuery 添加了 required 标志(也可以通过许多其他方式完成),并确保文本区域在更改后更新
要处理元素不可“聚焦”的问题,只需设置不透明度和高度,以便您可以删除display: none;
此 CSS 假定您在使用(for WordPress )输出时已将类添加smyles-add-required-attr
到编辑器editor_class
wp_editor
textarea[style*="display: none"].smyles-add-required-attr:invalid {
height: 0px !important;
opacity: 0 !important;
position: absolute !important;
display: flex !important;
margin-top: -75px;
}
这会在编辑器周围添加一个红色边框,假设它fieldset
在您的表单内部
.my-form fieldset:invalid .wp-editor-wrap {
border: 1px solid red;
}
一个潜在的问题是,在页面加载时,红色边框显示在周围fieldset
,您可以使用fieldset:invalid:focus-within
但要注意它对浏览器的支持有限https://caniuse.com/#feat=css-focus-within