简体   繁体   English

我应该怎样包装document.getElementById以便在找不到ID时不会引发TypeError?

[英]What do I wrap a document.getElementById so as not to throw a TypeError when ID can't be found?

I'm using the Hammer.js library ( http://hammerjs.github.io/ ) to enable touch gestures in a vanilla javascript environment. 我正在使用Hammer.js库( http://hammerjs.github.io/ )在原始javascript环境中启用触摸手势。 After including the library, I have a call that works on my gallery pages. 包含库之后,我将在画廊页面上进行通话。 The call fails should the id slide not be found on the page. 如果在页面上找不到ID幻灯片,则呼叫失败。 The resulting TypeError also prevents the other js for the site from being executed. 产生的TypeError还会阻止执行该站点的其他js。 I'm really new to this, but from doing some reading I think I need to wrap this code in a test so it won't run if id slide does not exist. 我真的很陌生,但是从阅读开始,我认为我需要将此代码包装在测试中,这样如果id slide不存在,它将不会运行。 Knowing how to do this will help me fix some other issues with other ids and libraries. 知道如何执行此操作将帮助我解决其他ID和库的其他问题。

So after the Hammer.js code I have my own code: 所以在Hammer.js代码之后,我有自己的代码:

var slide = document.getElementById('slide');
var mc = new Hammer(slide);
mc.on("swipeleft", function(ev) {
var url = document.querySelector('a.next').getAttribute('href');
if (url) {
    window.location = url;
}
});

mc.on("swiperight", function(ev) {
var url = document.querySelector('a.prev').getAttribute('href');
if (url) {
    window.location = url;
}
});

How do I prevent this code from executing if the page doesn't contain the id slide? 如果页面不包含id幻灯片,如何防止执行此代码? (and when I say it doesn't contain it, I mean that it will not exist on that page, not that the DOM isn't ready.) (当我说它不包含它时,我的意思是它不会在该页面上存在,不是说DOM还没有准备好。)

Thanks. 谢谢。

Just test for it: 只需测试一下:

var slide = document.getElementById('slide');

if( slide ){
  // .. your code
}

End the surrounding function execution if slide is falsy: 如果slide虚假,则结束周围的函数执行:

var slide = document.getElementById('slide');    
if (!slide) return;
//... The rest of your code

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我无法使用jQuery库,并且遇到TypeError:将“翻译”为纯JavaScript时document.getElementById(…)为null - I can't use jQuery library, and I am getting TypeError: document.getElementById(…) is null when 'translating' to pure javascript 如果找不到ID(document.getElementById),希望JS继续 - Want JS to continue if ID (document.getElementById) isn't found 当字段没有ID但有名称时,如何设置文本输入字段的值。 我无法使用document.getElementById - How do I set the value of a text input field when the field does not have an id but it has a name. I can't use document.getElementById 如果在没有ID的情况下使用document.getElementByID,它在做什么? - When document.getElementByID is used without an ID what is it doing? 调用document.getElementById时发生TypeError - TypeError when calling document.getElementById 为什么我的if语句不能以document.getElementById(“ ID”)。value = var结尾? - Why can't I end my if statement with document.getElementById(“ID”).value = var? 我可以在document.getelementbyID内传递多个ID吗? - Can I pass multiple Id's inside a document.getelementbyID? 为什么不能通过document.getElementById(“ node's_id”)。child选择子节点? - Why can't I select nodes child by document.getElementById(“node's_id”).child? document.getElementById无法使用,如何解决? - document.getElementById won’t work, how do I fix this? 更改document.getElementById时Ajax不起作用 - Ajax doesn't work when I change document.getElementById
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM