Javascript 如何处理getElementById返回Null
在我们的web应用程序中有许多页面。其中一些包含元素“Ribbon.ListForm.Display.Manage.Workflows-Medium”,而一些页面不包含元素 我想使用相同的脚本来检查所有页面。脚本将隐藏元素“Ribbon.ListForm.Display.Manage”、“Ribbon.ListForm.Display.Manage.Workflows Medium”和“Ribbon.ListForm.Display.Manage.CheckOut Large”(如果有)Javascript 如何处理getElementById返回Null,javascript,jquery,null,Javascript,Jquery,Null,在我们的web应用程序中有许多页面。其中一些包含元素“Ribbon.ListForm.Display.Manage.Workflows-Medium”,而一些页面不包含元素 我想使用相同的脚本来检查所有页面。脚本将隐藏元素“Ribbon.ListForm.Display.Manage”、“Ribbon.ListForm.Display.Manage.Workflows Medium”和“Ribbon.ListForm.Display.Manage.CheckOut Large”(如果有) 函数h
函数hideEdit(){
var edit=document.getElementById(“Ribbon.ListForm.Display.Manage”);
if(typeof edit!=“未定义”&&edit.value==“无”{edit.style.display=“无”;};
var wf=document.getElementById(“Ribbon.ListForm.Display.Manage.Workflows介质”);
if(typeof wf!=“未定义”&&wf.value==“无”{wf.style.display=“无”;};
var checkout=document.getElementById(“Ribbon.ListForm.Display.Manage.checkout-Large”);
if(typeof checkout!=“未定义”&&checkout.value==“无”{checkout.style.display=“无”};
}
问题是当一个页面不包含“Loop.ListFr.Stutial.Multual.WorkFraseMeod”(第二个元素),但包含“Loop.ListFr.Studio .Mulk.签出大”(第三个元素)时,脚本将以错误的方式停止在中间[Objor为NULL或unDebug ]。因此,第一个元素隐藏,但第三个元素不隐藏
你能告诉我如何修改我的剧本吗?谢谢。因为如果找不到元素,则返回null
元素是对对象的引用,如果是元素,则为null
文档中不存在具有指定ID的
您可以只检查truthy值,而不使用typeof test
if (edit && edit.value == ''){edit.style.display = "none";};
演示:,因为如果找不到元素,则返回null
元素是对对象的引用,如果是元素,则为null
文档中不存在具有指定ID的
您可以只检查truthy值,而不使用typeof test
if (edit && edit.value == ''){edit.style.display = "none";};
演示:您可以像这样检查空元素:
if (edit!=null && edit.value == '')
if (wf!=null && wf.value == '')
if (checkout!=null && checkout.value == '')
可以这样检查空元素:
if (edit!=null && edit.value == '')
if (wf!=null && wf.value == '')
if (checkout!=null && checkout.value == '')
因为问题是用jQuery标记的:
$('#Ribbon\.ListForm\.Display\.Manage,#Ribbon\.ListForm\.Display\.Manage\.Workflows-Medium,#Ribbon\.ListForm\.Display\.Manage\.CheckOut-Large')
.filter(function() {
return this.value == '';
})
.hide();
首先,它将选择您感兴趣的元素;然后,它将根据值隐藏与简单过滤器匹配的问题。因为问题用jQuery标记:
$('#Ribbon\.ListForm\.Display\.Manage,#Ribbon\.ListForm\.Display\.Manage\.Workflows-Medium,#Ribbon\.ListForm\.Display\.Manage\.CheckOut-Large')
.filter(function() {
return this.value == '';
})
.hide();
首先,它将选择您感兴趣的元素;然后,它将根据值隐藏与简单过滤器匹配的元素。即使该元素在页面中不存在,返回类型将为object,返回值将为null。 因此,您也可以检查null大小写。 请查看修改后的代码
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";}
}
谢谢,
varun.即使该元素在页面中不存在,返回类型也将是object,返回值将为null。 因此,您也可以检查null大小写。 请查看修改后的代码
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";}
}
谢谢,
瓦伦。我不确定为什么上面的选择器似乎不能工作。我不确定为什么上面的选择器似乎不能工作。