gpt4 book ai didi

DIV 的 Javascript Try Catch

转载 作者:行者123 更新时间:2023-11-28 20:08:21 25 4
gpt4 key购买 nike

我正在尝试构建一个 catch 尝试执行一些 javascript 代码。 NO JQUERY 我想确定 div id('test')是否存在,如果不存在,则不执行代码。我该怎么做?我知道 try catch 的结构是

try{

}
catch(e){

}

最佳答案

尽量少用try-catch:只检查DOM元素是否存在:

if (document.getElementById('test')!=null) {
// element (div) with id 'test' exists
}
else {
// it doesn't
}

关于DIV 的 Javascript Try Catch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20378600/

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