2 回答
TA貢獻1851條經驗 獲得超3個贊
原因是您使用的 css 賦值語法錯誤。控制臺會提醒您這一點。進行如下更改。
<script
src="https://code.jquery.com/jquery-3.5.1.min.js"
integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0="
crossorigin="anonymous"></script>
<body>
<p id="paragraph">This is a paragraph</p>
<button>Click me</button>
<script type="text/Javascript">
$("button").click(function(){
if ($("#paragraph").css("display") =="none"){
$("#paragraph").css("display", "block");
$("#paragraph").css("opacity", '1');
}
else{
$("#paragraph").fadeOut();
}
});
</script>
</body>
你可以忽略我是如何包含 jquery 的。我這樣做只是為了確保它在這里都可以運行。
TA貢獻1772條經驗 獲得超8個贊
使用快捷方式show()而不是css()讓它變得簡單
$("button").click(function() {
const $p = $("#paragraph");// cache element reference once for efficiency
if ($p.is(':hidden')) {
$p.show();
} else {
$p.fadeOut();
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p id="paragraph">This is a paragraph</p>
<button>Click me</button>
添加回答
舉報
