简体   繁体   English

如何设置document.getElementById(…)为null按摩为true

[英]How can I set document.getElementById(…) is null massage to true

I have more document.getElementById in my code, but when I run my code, just two document.getElementById (ctx1, ctx2) get values the others not. 我的代码中包含更多document.getElementById,但是当我运行代码时,只有两个document.getElementById(ctx1,ctx2)获取其他值。 How can I reach that the others (in my case ctx3) return true and I do not get error massage. 我如何才能使其他(在我的情况下为ctx3)返回true,而我却没有得到错误提示。 Here is my demo code: 这是我的演示代码:

DEMO DEMO

var ctx1=document.getElementById("data1").getContext("2d");     
var ctx2 =document.getElementById("data2").getContext("2d");
var ctx3 =document.getElementById("data3").getContext("2d");

In this case ctx1 and ctx2 get value but the ctx3 not, that's why I got the document.getElementById(...) is null error message in the console. 在这种情况下ctx1和ctx2获得值,但ctx3没有,这就是为什么我得到document.getElementById(...)在控制台中为空错误消息的原因。 I have some attempt but these are failed. 我做了一些尝试,但是都失败了。

ATTEMPT 尝试

1 1

if(ctx3 === null || 
   ctx3 === undefined) {
    return true;
}   

2 2

if(document.getElementById("data3")..getContext("2d").value == null){
    return true;
  }

If an element with that ID may not exist, you need to break up your statement. 如果具有该ID的元素可能不存在,则需要拆分语句。 You can't do this: 您不能这样做:

// Not this
var ctx3 =document.getElementById("data3").getContext("2d");

...because document.getElementById("data3") may return null , and then you get an error trying to use that for .getContext("2d") . ...因为document.getElementById("data3")可能返回null ,然后在尝试将其用于.getContext("2d")遇到错误。 So instead: 所以与其:

var elm3 = document.getElementById("data3");
var ctx3 = elm3 && elm3.getContext("2d");

Now, if there is no id="data3" element in your document, elm3 and ctx3 will both be null . 现在,如果您的文档中没有id="data3"元素,则elm3ctx3都将为null If there is, elm3 will refer to the element and ctx3 to the 2D context. 如果有, elm3将指元件和ctx3到2D的上下文。

If you're going to do this more than once, you could give yourself a function for it: 如果要多次执行此操作,则可以给自己一个函数:

function getOptionalContext(id) {
    var elm = document.getElementById(id);
    return elm && elm.getContext("2d");
}

Then: 然后:

var ctx1 = getOptionalContext("data1");
var ctx2 = getOptionalContext("data2");
var ctx3 = getOptionalContext("data3");

Side note: Any time you find yourself writing var1 , var2 , var3 , ..., consider using an array instead. 旁注:每当您发现自己编写var1var2var3 ,...时,请考虑使用数组。

If document.getElementById("data1") is undefined , ( document.getElementById("data1") != null ) will give false . 如果undefined document.getElementById("data1")( document.getElementById("data1") != null )将给出false In this case, you can easily skip the getContext("2d") if they are undefined . 在这种情况下,如果undefined getContext("2d")则可以轻松地跳过它们。 Hope this helps. 希望这可以帮助。

var ctx1 = ( document.getElementById("data1") != null ) ? ( document.getElementById("data1") != null ).getContext("2d") : null;
var ctx2 = ( document.getElementById("data2") != null ) ? ( document.getElementById("data2") != null ).getContext("2d") : null; 
var ctx3 = ( document.getElementById("data3") != null ) ? ( document.getElementById("data3") != null ).getContext("2d") : null; 

As mentioned above by others, the error occurs because .getElementById(...) method returns null if there is no element with such an ID. 如上面其他人所提到的,发生错误是因为如果没有具有此ID的元素, .getElementById(...)方法将返回null。 And you can't call methods on null , because it has no corresponding built-in class (for example, numbers have Number built-in class and that's why you can call methods like 3.0.toFixed() ). 而且您不能在null上调用方法,因为它没有相应的内置类(例如,数字具有Number内置类,因此您可以调用3.0.toFixed()类的方法)。 Taking into account great suggestion of TJ Crowder about using an array, here is my code: 考虑到TJ Crowder关于使用数组的绝妙建议,这是我的代码:

var selectors = ["data1", "data2", "data3"];
var contexts = selectors.map(function(selector) {
    var elem = document.getElementById(selector);
    return elem ? elem.getContext("2d") : true;
});

This will return an array of contexts (if element is not present, on the place of its context in the array, true will be present) 这将返回一个上下文数组(如果不存在元素,则在数组中其上下文的位置出现true

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM