我编写了一个复制器脚本,它克隆了之前的最后一个字段组。我的问题是,单击克隆的WP颜色选择器字段时不会打开新的光谱,而是会打开从中克隆的颜色选择器组中的颜色选择器。
当然,一旦您保存字段并重新加载页面,它就可以正常工作,但在克隆之前的组时,它不会即时工作。
以下是HTML字段:
<div class="md-repeat">
<a href="#" class="md-repeat-add">Add Another Color Picker</a>
<div class="md-repeat-field">
<label for="my_field">My Color Picker Field</label>
<input name="my_field" id="my_field" type="text" class="my-colorpicker" />
</div>
</div>
以及转发器脚本:
...
add: function() {
$( ".md-repeat-add" ).click( function( e ) {
e.preventDefault();
var lastRepeatingGroup = $( this ).closest( \'.md-repeat\' ).find( \'.md-repeat-field\' ).last(),
clone = lastRepeatingGroup.clone( true );
clone.find( \'input.regular-text, textarea, select\' ).val( \'\' );
clone.find( \'input.regular-text\' ).prop( \'readonly\', false );
clone.find( \'input[type=checkbox]\' ).attr( \'checked\', false );
clone.insertAfter( lastRepeatingGroup );
MD_Repeatable_Fields.resetAtts( clone );
});
},
resetAtts: function( section ) {
var attrs = [ \'for\', \'id\', \'name\' ],
tags = section.find( \'input, label, textarea, select\' ),
count = section.index();
tags.each( function() {
var $this = $( this );
$.each( attrs, function( i, attr ) {
var attr_val = $this.attr( attr );
if ( attr_val )
$this.attr( attr, attr_val.replace( /(\\d+)(?=\\D+$)/, count ) );
});
});
},
...
如何“重置”颜色选择器脚本以检测何时动态创建新的颜色选择器,并在单击时执行适当的JS以打开新的颜色选择器?
SO网友:Paul \'Sparrow Hawk\' Biron
很难确定,因为您没有包括为metabox输出的标记,也没有包括用于克隆的JS。但是,以下简化设置似乎适合我:
metabox markup
<label for=\'my_field\'>
My Color Picker Field
</label>
<input name=\'my_field\' id=\'my_field\' type=\'text\' class=\'my-colorpicker\' />
<a href=\'#\' class=\'clone-it\'>Add Another Color Picker</a>
JS
(function ($) {
// turn the field into a colorpicker
$(\'.my-colorpicker\').wpColorPicker () ;
$(\'.clone-it\').click (function () {
// clone the field in question
var field = $(this).prev (\'.wp-picker-container\').find (\'.my-colorpicker\').clone () ;
// set the cloned field\'s value to \'\'
// so it starts off without a specific color
$(field).val (\'\') ;
// your code to mod the cloned field\'s name, etc goes here
// add the cloned field to the metabox
$(this).before (field) ;
// turn the cloned field into a colorpicker
$(field).wpColorPicker () ;
}) ;
})(jQuery)