简体   繁体   English

如何处理getElementById返回Null

[英]How to handle getElementById return Null

On our web application there are many pages. 在我们的Web应用程序上有很多页面。 Some of them contain element "Ribbon.ListForm.Display.Manage.Workflows-Medium" while some pages not. 其中一些包含元素“ Ribbon.ListForm.Display.Manage.Workflows-Medium”,而某些页面则不包含。

I would like to use same script to check against all pages. 我想使用相同的脚本来检查所有页面。 The script will hide the element "Ribbon.ListForm.Display.Manage", "Ribbon.ListForm.Display.Manage.Workflows-Medium" and "Ribbon.ListForm.Display.Manage.CheckOut-Large" if any. 脚本将隐藏元素“ Ribbon.ListForm.Display.Manage”,“ Ribbon.ListForm.Display.Manage.Workflows-Medium”和“ Ribbon.ListForm.Display.Manage.CheckOut-Large”。

 function hideEdit() {
        var edit = document.getElementById("Ribbon.ListForm.Display.Manage");
        if (typeof edit !== "undefined" && edit.value == ''){edit.style.display = "none";};
        var wf = document.getElementById("Ribbon.ListForm.Display.Manage.Workflows-Medium");
        if (typeof wf !== "undefined" && wf.value == ''){wf.style.display = "none";};
        var checkout = document.getElementById("Ribbon.ListForm.Display.Manage.CheckOut-Large");
        if (typeof checkout !== "undefined" && checkout.value == ''){checkout.style.display = "none";};
}       

The problem is when a page does not contain "Ribbon.ListForm.Display.Manage.Workflows-Medium" (the 2nd element) but contains "Ribbon.ListForm.Display.Manage.CheckOut-Large" (the 3rd element), the script will stop at in the middle with error [object is null or undefined]. 问题是当页面不包含“ Ribbon.ListForm.Display.Manage.Workflows-Medium”(第二个元素)但包含“ Ribbon.ListForm.Display.Manage.CheckOut-Large”(第三个元素)时,脚本将在错误处停止在中间[对象为null或未定义]。 Hence, 1st element is hided but 3rd element is not. 因此,第一个元素被隐藏,而第三个元素则未被隐藏。

Could you please advice how to amend my script? 您能否建议如何修改我的脚本? Thank you. 谢谢。

Because getElementById() returns null if the element is not found. 因为如果找不到元素,则getElementById()返回null。

element is a reference to an Element object, or null if an element with the specified ID is not in the document. element是对Element对象的引用;如果具有指定ID的元素不在文档中,则为null。

You can just check for the truthy value instead of use the typeof test 您可以只检查真实值,而不必使用typeof测试

if (edit && edit.value == ''){edit.style.display = "none";};

Demo: Fiddle 演示: 小提琴

You can check like this for null element: 您可以像这样检查null元素:

if (edit!=null && edit.value == '') 
if (wf!=null && wf.value == '')
if (checkout!=null && checkout.value == '')

Since the question is tagged with jQuery: 由于问题是用jQuery标记的:

$('#Ribbon\.ListForm\.Display\.Manage,#Ribbon\.ListForm\.Display\.Manage\.Workflows-Medium,#Ribbon\.ListForm\.Display\.Manage\.CheckOut-Large')
    .filter(function() {
        return this.value == '';
    })
    .hide();

First, it will select the elements you're interested in; 首先,它将选择您感兴趣的元素; then, it will hide those that match a simple filter based on value. 然后,它将隐藏与基于值的简单过滤器匹配的过滤器。

Even if the element is not existing in the page, the return type will be object and return value will be null. 即使页面中不存在该元素,返回类型也将是object并且返回值将为null。 so, you can check the null case also. 因此,您也可以检查null大小写。 please see the modified code. 请参阅修改后的代码。

function hideEdit() {
    var edit = document.getElementById("Ribbon.ListForm.Display.Manage");
    if ( edit != null && typeof edit !== "undefined" && edit.value == ''){edit.style.display = "none";};
    var wf = document.getElementById("Ribbon.ListForm.Display.Manage.Workflows-Medium");
    if (wf != null && typeof wf !== "undefined" && wf.value == ''){wf.style.display = "none";}
    var checkout = document.getElementById("Ribbon.ListForm.Display.Manage.CheckOut-Large");
    if (checkout != null && typeof checkout !== "undefined" && checkout.value == ''){checkout.style.display = "none";}

} }

thanks, varun. 谢谢,瓦伦。

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

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