在我们的Web应用程序中有许多页面。其中一些包含元素“Ribbon.ListForm.Display.Manage.Workflows-Medium”,而一些页面则不包含。如何处理getElementById返回空
我想用同样的脚本来检查对所有页面。该脚本将隐藏“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";};
}
问题是,当一个页面中不包含“Ribbon.ListForm.Display.Manage.Workflows介质”(第2元素),但包含“Ribbon.ListForm.Display.Manage.CheckOut,大“(第三个元素),脚本将停在中间,出错[object is null or undefined]。因此,第一个元素是隐藏的,但第三个元素不是。
请您指点如何修改我的脚本?谢谢。
我不知道为什么,但上面的选择器似乎无法正常工作。 – Mark 2015-04-01 03:32:59
@Mark更新了我的答案来解决这个问题,谢谢! – 2015-04-01 06:38:10