【问题标题】:How can I hide one div in Wordpress using jQuery, and show it when another div is hidden?如何使用 jQuery 在 Wordpress 中隐藏一个 div,并在隐藏另一个 div 时显示它?
【发布时间】:2019-10-19 13:42:41
【问题描述】:

不知何故,这不起作用。默认情况下应该隐藏#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
);

}

【问题讨论】:

  • if() 也需要在ready()

标签: php jquery html wordpress


【解决方案1】:

您可以使用如下内联样式:

<div id='autoverkocht' style='display: none;'>
</div>

或像这样使用 css 样式:

#autoverkocht{
  display: none;
}

在 jquery 中这样做:

<script>
  jQuery(document).ready(function($){ 
    if($('.b-car-info__price).is(":hidden")){
      $('#autoverkocht').show();        
    }
  });

</script>

【讨论】:

  • 使用jQuery(document).ready(function($){很重要,因为wordpress使用noConflict()
  • 使用验证器检查代码,但返回:Error: Line 2: Unexpected token ILLEGAL?
  • 第 2 行指的是哪一行?
【解决方案2】:

认为 jQuery togglehide 是您所需要的。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-26
    • 2012-02-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多