Javascript 在设置更新时更新自定义滑块
我正在为WPBakery页面生成器插件创建一个自定义滑块元素,因为内置的旋转木马不适合我的用途 除了更新元素设置外,一切都很正常,滑块消失了,因为元素是通过删除旧的HTML在DOM中重新创建的 以下是元素代码:Javascript 在设置更新时更新自定义滑块,javascript,jquery,wordpress,visual-composer,Javascript,Jquery,Wordpress,Visual Composer,我正在为WPBakery页面生成器插件创建一个自定义滑块元素,因为内置的旋转木马不适合我的用途 除了更新元素设置外,一切都很正常,滑块消失了,因为元素是通过删除旧的HTML在DOM中重新创建的 以下是元素代码: <?php class WPBakery_Custom_Slider { /** * Setup class */ public function __construct() { // Actions. add_a
<?php
class WPBakery_Custom_Slider {
/**
* Setup class
*/
public function __construct() {
// Actions.
add_action( 'vc_before_init', array( $this, 'custom_mapping' ) );
// Shortcodes.
add_shortcode( 'wpbakery_custom_slider', array( $this, 'shortcode' ) );
}
/**
* Custom Mapping
*/
public function custom_mapping() {
vc_map( array(
'name' => __( 'Custom Slider', 'text-domain' ),
'base' => 'wpbakery_custom_slider',
'description' => __( 'The Custom Slider.', 'text-domain' ),
'category' => __( 'Content', 'text-domain' ),
'icon' => 'icon-wpb-carousel',
'front_enqueue_js' => get_theme_file_uri( 'assets/js/wpbakery-page-builder.js' ),
'params' => array(
...
),
) );
}
/**
* The element HTML
*
* @param array $atts Shortcode attributes.
* @return string
*/
public function shortcode( $atts ) {
// Params extraction.
...
ob_start();
?>
<div class="text-domain-wpb-custom-slider-wrapper">
<div class="text-domain-wpb-custom-slider">
...
</div>
</div>
<?php
$html = ob_get_clean();
return $html;
}
}
但是当事件运行时,似乎.text-domain wpb自定义滑块不存在。我尝试的变体有:
vc.events.on( 'shortcodes:wpbakery_custom_slider:update', function ( model ) {
jQuery( '.text-domain-wpb-custom-slider', document ).customSlider();
} );
及
保存元素设置时,上述所有操作将导致:
Uncaught TypeError: jQuery(...).customSlider is not a function
因为jQuery('.text-domain-wpb-custom-slider',document).length
为零,即使DOM中有一个.text-domain-wpb-custom-slider
关于如何正确更新WPBakery Page Builder/Visual Composer的自定义基于JavaScript的元素,您有什么想法吗?问题在于文档
变量的范围。它指的是生成器编辑页面,而不是需要编辑的页面(WPBakery称为model
)
下面是解决方案,使用model.view.el
而不是document
来参考页面:
vc.events.on( 'shortcodes:wpbakery_custom_slider:update', function ( model ) {
setTimeout( function () {
jQuery( model.view.el ).find( '.text-domain-wpb-custom-slider' ).customSlider();
}, 1000 );
} );
Uncaught TypeError: jQuery(...).customSlider is not a function
vc.events.on( 'shortcodes:wpbakery_custom_slider:update', function ( model ) {
setTimeout( function () {
jQuery( model.view.el ).find( '.text-domain-wpb-custom-slider' ).customSlider();
}, 1000 );
} );