Mik*_*ike 0 html javascript css
我的一个SSI文件中有一个javascript事件处理程序,它隐藏了一些元素,这些元素可能会或可能不会出现在调用SSI文件的页面上.结果,处理程序顺利进行,直到它找不到我要求它隐藏的元素,然后在完成任何可能隐藏的内容之前停止.那么,我怎样才能让JavaScript不会挂断该元素是否在页面上并且只是通过处理程序?
非常感谢.
麦克风
取决于你如何获得元素.如果您没有使用JS库,那么您可以执行以下操作:
/* handler begin code */
var el = document.getElementById('myElement');
if (el) {
//do work
}
/* handler end code */
Run Code Online (Sandbox Code Playgroud)
如果你使用像jQuery这样的东西你可以更容易地选择元素,然后即使它不存在,所选集合上的任何操作都不会抛出异常
//this will work even if myElement doesn't exist - no exceptions will be thrown
$("#myElement").hide();
Run Code Online (Sandbox Code Playgroud)