在jquery中隐藏段落元素

时间:2015-04-13 17:06:27

标签: jquery jquery-ui jquery-mobile jquery-plugins

我需要隐藏Query中的元素段落,但我希望隐藏元素段落并保留其位置,下面的按钮不代替段落

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("#hide").click(function(){
        $("p").hide();
    });
    $("#show").click(function(){
        $("p").show();
    });
});
</script>
</head>
<body>

<p>If you click on the "Hide" button, I will disappear.</p>

<button id="hide">Hide</button>
<button id="show">Show</button>

</body>
</html>

4 个答案:

答案 0 :(得分:2)

试试这个

$(document).ready(function(){
    $("#hide").click(function(){
        $("p").css('visibility', 'hidden');
    });
    $("#show").click(function(){
        $("p").css('visibility', 'visible');
    });
});

答案 1 :(得分:2)

<!DOCTYPE html>
<html>
<head>
  <style>p{visibility:hidden;}</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("#hide").click(function(){
        $("p").css('visibility','hidden'); // to hide
    });
    $("#show").click(function(){
        $("p").css('visibility','visible'); // to show
    });
});
</script>
</head>
<body>

<p>If you click on the "Hide" button, I will disappear.</p>

<button id="hide">Hide</button>
<button id="show">Show</button>

</body>
</html>

您可以添加样式:

<head>
    <style>
        p{visibility:hidden;}
    </style>
</head>

和js:

$("p").css('visibility','visible'); // to show
$("p").css('visibility','hidden'); // to hide

答案 2 :(得分:2)

您可以使用“visibility”CSS属性:

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("#hide").click(function(){
        $("p").css('visibility', 'hidden');
    });
    $("#show").click(function(){
        $("p").css('visibility', 'visible');
    });
});
</script>
</head>
<body>

<p>If you click on the "Hide" button, I will disappear.</p>

<button id="hide">Hide</button>
<button id="show">Show</button>

</body>
</html>

答案 3 :(得分:1)

尝试使用visibility:hidden insted,它会隐藏元素可见性,但不会隐藏位置:

$(document).ready(function(){
    $("#hide").click(function(){
        $("p").css('visibility', 'hidden');
    });
    $("#show").click(function(){
        $("p").css('visibility', 'visible');
    });
});