Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 使用切换隐藏和取消隐藏页面部分_Jquery_Html - Fatal编程技术网

Jquery 使用切换隐藏和取消隐藏页面部分

Jquery 使用切换隐藏和取消隐藏页面部分,jquery,html,Jquery,Html,我有这把小提琴,我试图隐藏一篇博客文章,只显示博客标题和摘要(摘录)。要做到这一点,我用这种方式切换 $('.entire_blog_post').css('display','none'); $('.the_post_title').toggle( function(){ $('.the_post_teaser').css('display','none'); $('.entire_blog_post').css('display',''); }, function(){ $('

我有这把小提琴,我试图隐藏一篇博客文章,只显示博客标题和摘要(摘录)。要做到这一点,我用这种方式切换

$('.entire_blog_post').css('display','none');

$('.the_post_title').toggle(
  function(){
$('.the_post_teaser').css('display','none');
$('.entire_blog_post').css('display','');
  },
  function(){
$('.the_post_teaser').css('display','');
$('.entire_blog_post').css('display','none');
  }
);
我用这个代码段实现了它,但在这条路上的某个地方,我不得不拥有一个专用的css类

$('.the_post_title').on('click',function(){
$('.the_post_teaser').css('display','none');
$('.entire_blog_post').css('display','');
});

如何使用
toggle()

解决这个问题您可以创建一个或两个函数,如果愿意,只需调用它即可

function showpost(){
   $('.the_post_teaser').hide();
   $('.entire_blog_post').show();
}

function hidepost(){
    $('.the_post_teaser').show();
    $('.entire_blog_post').hide();
}
然后是点击器

$('.the_post_title').click(function(){
    if($('.the_post_teaser').css('display') == 'block'){
        showpost();
    } else {
        hidepost();
    }
});
工作

请尝试以下方法:

$('.entire_blog_post').css('display','none');

$('.the_post_title').click( function() {
  $('.the_post_teaser').toggle();
  $('.entire_blog_post').toggle();
});

您也可以将style=“display:none;”添加到div中,使用类“整篇blog\u post”,而不是使用js添加该属性。

我通过if
if


您可以使用
hide()
show()
功能更改显示。另外,当你使用css时,你会将显示更改为
,以显示元素。我已经尝试过了,这只会使它永远上下移动。当你点击标题时,它会切换(在显示摘录和文章之间切换)。你想做什么?它解决了问题,但它无法控制地切换,永远不会停止。你是说它一直在为你显示和隐藏?你使用的代码和我写的完全一样吗?这样,它只在你点击的时候才切换。好吧,你的答案可以,但在我的例子中,我在切换中做了一些其他的事情。当然可以。很好。我已经竖起了大拇指。
$('.the_post_title').on('click', function(){
    if (!$(this).attr('data-toggled') || $(this).attr('data-toggled') == 'off'){

           $(this).attr('data-toggled','on');
           $('.the_post_teaser').css('display','none');
$('.entire_blog_post').css('display','');
    }
    else if ($(this).attr('data-toggled') == 'on'){
           $(this).attr('data-toggled','off');
           $('.the_post_teaser').css('display','');
$('.entire_blog_post').css('display','none');
    }
});