How to change css display none or block property u

2019-01-04 16:18发布

How to change css display none or block property using Jquery?

11条回答
淡お忘
2楼-- · 2019-01-04 16:42
(function($){
    $.fn.displayChange = function(fn){
        $this = $(this);
        var state = {};
        state.old = $this.css('display');
        var intervalID = setInterval(function(){
            if( $this.css('display') != state.old ){
                state.change = $this.css('display');
                fn(state);
                state.old = $this.css('display');
            }
        }, 100);        
    }

    $(function(){
        var tag = $('#content');
        tag.displayChange(function(obj){
            console.log(obj);
        });  
    })   
})(jQuery);
查看更多
唯我独甜
3楼-- · 2019-01-04 16:47

Other way to do it using jQuery CSS method:

$("#id").css({display: "none"});
$("#id").css({display: "block"});
查看更多
淡お忘
4楼-- · 2019-01-04 16:51

In case you want to hide and show an element depending on whether it is already visible or not you can use toggle instead: of .hide() and .show()

$('elem').toggle();

查看更多
叛逆
5楼-- · 2019-01-04 16:55

Simple way:

function displayChange(){
$(content_id).click(function(){
  $(elem_id).toggle();}

)}
查看更多
家丑人穷心不美
6楼-- · 2019-01-04 16:56

There are several ways to accomplish this, each with it's own intended purpose.


1.) To use inline while simply assigning an element a list of things to do

$('#ele_id').css('display', 'block').animate(....
$('#ele_id').css('display', 'none').animate(....

2.) To use while setting multiple CSS Properties

$('#ele_id').css({
    display: 'none'
    height: 100px,
    width: 100px
});
$('#ele_id').css({
    display: 'block'
    height: 100px,
    width: 100px
});

3.) To dynamically call on command

$('#ele_id').show();
$('#ele_id').hide();

4.) To dynamically toggle between block and none, if it's a div

  • some elements are displayed as inline, inline-block, or table, depending on the Tag Name

$('#ele_id').toggle();

查看更多
登录 后发表回答