首页 > 解决方案 > 如何使用 jQuery 在 Wordpress 中隐藏一个 div,并在隐藏另一个 div 时显示它?

问题描述

不知何故,这不起作用。默认情况下应该隐藏#autooverkocht,除非隐藏 .b-car-info__price,否则应该显示#autooverkocht。

jQuery(document).ready(function($){
$("#autoverkocht").css({   
    display: "none",
    visibility: "hidden"
});
})

if($('.b-car-info__price').is(":hidden")){


      $('#autoverkocht').css('visibility', 'visible');
}

要么是上面的 jQuery,要么是下面的 functions.php 代码不起作用。

add_action( 'wp_enqueue_scripts', 'add_my_script' );
function add_my_script() {
    wp_enqueue_script(
    'your-script', // name your script so that you can attach other scripts and de-register, etc.
    get_template_directory_uri() . '/js/autoverkocht.js', // this is the location of your script file
    array('jquery') // this array lists the scripts upon which your script depends
);

}

标签: phpjqueryhtmlwordpress

解决方案


认为 jQuery切换隐藏是您所需要的。

$('#autoverkocht').hide()
$('#autoverkocht, .b-car-info__price').on(
  'click',
 function() 
 {
   $('#autoverkocht, .b-car-info__price').toggle()
 }
);

希望这是你需要的......


推荐阅读