gpt4 book ai didi

javascript - Jquery 函数 - 元素上

转载 作者:行者123 更新时间:2023-12-03 12:03:54 32 4
gpt4 key购买 nike

我有这个 jquery 代码,100% 工作。当我在 html 中导入 js 文件时,这会在每个页面加载时执行。我的问题是,仅当元素存在时,我如何使用此代码,或者直接从元素本身调用它?我有类似的功能,但是,在没有特定元素的 html 上,javascript 执行会停止。有时是因为该特定 html 文件中不存在 html 元素。

/**
* Message Box Display
*/
$(document).ready(function() {
$("#Message-Box" ).slideDown("slow", function() {
$("#Message-Box").addClass('Show');
setTimeout(function(){
$('#Message-Box').addClass('Hide');
}, 5000);
});
});

最佳答案

尝试这样的事情

$(document).ready(function() {
var msgbx = $("#Message-Box");
if (msgbx.length) {
msgbx.slideDown("slow", function() {
msgbx.addClass('Show');
setTimeout(function() {
msgbx.addClass('Hide');
}, 5000);
});
}
});

关于javascript - Jquery 函数 - 元素上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25272296/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com