早上好,我經歷了很多類似的問題和解決方案,但仍然無法用 Jquery 隱藏 div。
我的 div 在用戶控制元件內,它在 gridview 更新后顯示“成功”訊息。我不希望那個 div 是持久的,并且想在幾秒鐘后隱藏它而不重繪 頁面。這是我的代碼:
<div id="MsgInfo" runat="server" class="divMsg" visible="false" /></div>
在我的用戶控制元件中,我添加了腳本檔案的鏈接:
<script type="text/javascript" src="../HideMsg.js"></script>
劇本:
$(function () {
setTimeout(function () { $("#MsgInfo").fadeOut(1500); }, 5000);
});
我不知道那里缺少什么,但訊息不能淡出或消失。
感謝您的幫助。
uj5u.com熱心網友回復:
檢查這個作業示例:codepen
在更新點擊里面你需要使用 setTimeout
$("#showDiv").click(function () {
$("#MsgInfo").fadeIn(1500);
setTimeout(function () {
$("#MsgInfo").fadeOut(1500);
}, 5000);
});
CSS:
#MsgInfo {
display: none
}
uj5u.com熱心網友回復:
使用class名稱而不是id. 原因是您正在使用asp.netcontrol GridvViewwith runat server。所以它將更新 id 的值(嘗試檢查瀏覽器上的元素并檢查id)。因此,不要使用id選擇器,而是嘗試使用class選擇器。嘗試如下。
$(function () {
setTimeout(function () { $(".divMsg").fadeOut(1500); }, 5000);
});
你也可以在這里參考這個答案:Getting ID from asp.net runat server in jQuery
uj5u.com熱心網友回復:
如果頁面加載后出現#MsgInfo div,則嘗試在#MsgInfo div 中添加腳本。
<script>
const myTimeout = setTimeout(myMsgInfo, 5000);
function myMsgInfo() {
var x = document.getElementById("MsgInfo");
x.style.display = "none";
}
</script>
uj5u.com熱心網友回復:
問題在于您的 div 結束標簽使用<div>..</div>不<div/>...</div>
$(function () {
setTimeout(function () { $("#MsgInfo").fadeOut(1500); }, 5000);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="MsgInfo" runat="server" class="divMsg" visible="false">It will hide after 5 seconds</div>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/466398.html
標籤:jQuery
上一篇:如何根據文本框值附加和洗掉div
