jQuery和hiding:after /:之前的伪类 [英] jQuery and hiding :after / :before pseudo classes

查看:142
本文介绍了jQuery和hiding:after /:之前的伪类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

jQuery和伪类


我试图隐藏:类通过jQuery在许多方面,但没有成功,我找到了另一个解决方案,通过添加一个空的div在我的div,包含:after内容,然后完全隐藏div,所以它给出相同的效果。 p>

然而,我只是好奇,如果有人设法找到一种方式隐藏:后或:之前的东西。这是我试过的没有工作。

  $('search_box:after')。 
$('。search_box:after')。css('display','none');

只是为了给你上下文,我有一个div包含搜索表单等,搜索是活跃的我想启用一个小的箭头指示符下的div指向找到的项目列表(构建:通过CSS后),当没有找到,或默认情况下,完整的列表(因为没有找到)然后我想隐藏它。

解决方案

你不能这样做。最好的办法是使用元素上的类来切换伪元素的显示。

 < style> 
.search_box:after {
content:'foo';
}
.search_box.hidden:after {
display:none;
}
< / style>
< script>
//而不是这两行
// $('。search_box:after')。hide();
//$('search_box:after').css('display','none');

//使用
$('。search_box')。addClass('hidden');
< / script>

活动示例 - http://jsfiddle.net/4nbnh/


Possible Duplicate:
jQuery and pseudo-classes

I tried hiding the :after pseudo class via jQuery in a number of ways but with no success, I've found another solution by adding a sort of empty div under my div that contains the :after content and then hiding the div entirely so it gives the same effect.

However I was just curious if anyone had managed to find a way to hide the :after or :before stuff. Here is what I tried that didn't work.

$('.search_box:after').hide();
$('.search_box:after').css('display', 'none');

Just to give you the context, I have a div that contains the search form etc, and when the search is active I want to enable a little arrow indicator under the div pointing like to the list of found items (built with :after via CSS) and when nothing is found, or it defaults to the full out list (since nothing was found) then I want to hide it.

解决方案

You cannot do this. Your best bet is to use a class on the element to toggle the display of the pseudo element.

<style>
    .search_box:after {
        content: 'foo';
    }
    .search_box.hidden:after {
        display: none;
    }
</style>
<script>
    //Instead of these 2 lines
    //$('.search_box:after').hide();
    //$('.search_box:after').css('display', 'none');

    //Use
    $('.search_box').addClass('hidden');
</script>

Live example - http://jsfiddle.net/4nbnh/

这篇关于jQuery和hiding:after /:之前的伪类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆