更改显示隐藏脚本,使其在 IE 上运行
我正在使用此脚本来显示/隐藏页面上的菜单部分,但它似乎不适用于 Internet Explorer。如何更改代码以便它也可以在 IE 上运行?
我认为这可以在 Jquery 中使用切换功能更轻松地完成,但我想保持相同的结构,任何人都可以帮助我吗?
visibleDiv1 = "";
visibleDiv2 = "";
visibleDiv3 = "";
visibleDiv4 = "";
function showHide1(elementid1){
if (document.getElementById(elementid1).style.display == 'none'){
document.getElementById(elementid1).style.display = '';
if(visibleDiv1 != ""){
if(visibleDiv1 != elementid1){
document.getElementById(visibleDiv1).style.display = 'none';
}
}
visibleDiv1 = elementid1;
} else {
document.getElementById(elementid1).style.display = 'none';
}
}
function showHide2(elementid2){
if (document.getElementById(elementid2).style.display == 'none'){
document.getElementById(elementid2).style.display = '';
if(visibleDiv2 != ""){
if(visibleDiv2 != elementid2){
document.getElementById(visibleDiv2).style.display = 'none';
}
}
visibleDiv2 = elementid2;
} else {
document.getElementById(elementid2).style.display = 'none';
}
}
function showHide3(elementid3){
if (document.getElementById(elementid3).style.display == 'none'){
document.getElementById(elementid3).style.display = '';
if(visibleDiv3 != ""){
if(visibleDiv3 != elementid3){
document.getElementById(visibleDiv3).style.display = 'none';
}
}
visibleDiv3 = elementid3;
} else {
document.getElementById(elementid3).style.display = 'none';
}
}
function showHide4(elementid4){
if (document.getElementById(elementid4).style.display == 'none'){
document.getElementById(elementid4).style.display = '';
if(visibleDiv4 != ""){
if(visibleDiv4 != elementid4){
document.getElementById(visibleDiv4).style.display = 'none';
}
}
visibleDiv4 = elementid4;
} else {
document.getElementById(elementid4).style.display = 'none';
}
}
I'm using this script to show/hide menu sections on my page but it doesn't seem to work on Internet Explorer. How can I change the code so that it works on IE as well?
I think that this could be done in Jquery much more easily with the toggle function but I'd like to keep the same structure, can anyone help me out?
visibleDiv1 = "";
visibleDiv2 = "";
visibleDiv3 = "";
visibleDiv4 = "";
function showHide1(elementid1){
if (document.getElementById(elementid1).style.display == 'none'){
document.getElementById(elementid1).style.display = '';
if(visibleDiv1 != ""){
if(visibleDiv1 != elementid1){
document.getElementById(visibleDiv1).style.display = 'none';
}
}
visibleDiv1 = elementid1;
} else {
document.getElementById(elementid1).style.display = 'none';
}
}
function showHide2(elementid2){
if (document.getElementById(elementid2).style.display == 'none'){
document.getElementById(elementid2).style.display = '';
if(visibleDiv2 != ""){
if(visibleDiv2 != elementid2){
document.getElementById(visibleDiv2).style.display = 'none';
}
}
visibleDiv2 = elementid2;
} else {
document.getElementById(elementid2).style.display = 'none';
}
}
function showHide3(elementid3){
if (document.getElementById(elementid3).style.display == 'none'){
document.getElementById(elementid3).style.display = '';
if(visibleDiv3 != ""){
if(visibleDiv3 != elementid3){
document.getElementById(visibleDiv3).style.display = 'none';
}
}
visibleDiv3 = elementid3;
} else {
document.getElementById(elementid3).style.display = 'none';
}
}
function showHide4(elementid4){
if (document.getElementById(elementid4).style.display == 'none'){
document.getElementById(elementid4).style.display = '';
if(visibleDiv4 != ""){
if(visibleDiv4 != elementid4){
document.getElementById(visibleDiv4).style.display = 'none';
}
}
visibleDiv4 = elementid4;
} else {
document.getElementById(elementid4).style.display = 'none';
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这确实是编码很差,您需要重新使用您的代码,并简单地调用您想要隐藏的元素。您已经为每个元素创建了函数。
您应该完成这样的主要函数:
然后简单地调用该函数执行类似的操作:
我真的建议使用 jQuery,因为这个简单隐藏的东西开箱即用得到更好的支持,并且你可以专注于其他事情。使用
toggle()
或简单的.hide()
/.show()
函数。This is really poorly coded, you need to re-use your code, and simply call the elements you want to hide. You have created functions for EACH element.
You should have done a primary function like this:
and later simply call that function doing something like:
I would really suggest grabbing jQuery tho, as this simple hidding stuff is better supported right out of the box, and you can focus on other things. Use
toggle()
or just plain.hide()
/.show()
functions.