简体   繁体   English

显示和隐藏div的javascript在IE中有效,但在FF或Chrome中不起作用

[英]Show and Hide div's javascript works in IE but not FF or Chrome

I have a number of spans(20 - to be exact) they all have Id's. 我有一些跨度(准确地说是20),它们都具有ID。 I then in my style sheet I set the visibility to hidden. 然后,在样式表中将可见性设置为“隐藏”。

I made this script to show and hide these spans once clicking on a section of the image map: 单击图像地图的一部分后,我制作了此脚本来显示和隐藏这些跨度:

function showDiv(pass) {
    var divs = document.getElementsByTagName('span');    
    for (i = 0; i < divs.length; i++) {
        if (divs[i].id.match(pass)) {
            (pass).style.visibility = 'visible';
            divs[i].style.visibility = 'hidden';
        }          
    }
} 

It works perfectly in IE but FF doesn't budge, Chrome displays it alright enough with minor problems that I think I can fix. 它可以在IE中完美运行,但FF不会让步,Chrome可以正常显示它并存在一些小问题,我认为我可以解决。

Anyone know why FF doesn't accept? 有人知道FF为什么不接受吗? Any suggestions would be greatly appreciated and compensated for in the after life :) 任何建议将不胜感激,并在以后的生活中补偿:)

style.display = 'none';
style.display = 'inline';

EDIT 编辑

Here is my source 这是我的资料

http://www.w3schools.com/css/pr_class_display.asp http://www.w3schools.com/css/pr_class_display.asp

It is because you are passing in a string, and then later using it as an element reference. 这是因为您要传入一个字符串,然后再将其用作元素引用。 IE must be smart enough to search the DOM and find an element with an ID matching the string. IE必须足够聪明才能搜索DOM并找到ID匹配字符串的元素。

Try this... 尝试这个...

if (divs[i].id.match(pass)) {
    document.getElementById(pass).style.visibility = 'visible';
    divs[i].style.visibility = 'hidden';
}

Or it could be that the String.match function is expecting a RegExp. 或者,它可能是该String.match函数需要一个正则表达式。 If the above does not resolve your problem, try this... 如果以上方法仍不能解决您的问题,请尝试此...

if (divs[i].id.match(new RegExp(pass, 'gi'))) {

pass is an ID. pass是一个ID。 In Firefox, element ids in the DOM aren't available as global references. 在Firefox中,DOM中的元素ID不能用作全局引用。 You can't do id .style.visibility = 'visible'; 你不能做id .style.visibility = 'visible'; Instead your function should look something like this: 相反,您的函数应如下所示:

function showDiv(pass) {
    var divs = document.getElementsByTagName('span');    
    for (i = 0; i < divs.length; i++) {
        if (divs[i].id.match(pass)) {
            divs[i].style.visibility = 'hidden';
        }          
    }
    document.getElementById(pass).style.visibility = 'visible';
}

You should be able to set the visibility of pass outside the loop since you only need to do it once. 您应该可以设置循环外passvisibility ,因为您只需要执行一次。

    if (divs[i].id.match(pass)) {

Is pass really a regular expression? pass真的是一个正则表达式吗? Seems unusual to be using match here. 在这里使用match似乎很不寻常。

        (pass).style.visibility = 'visible';

This makes no sense whether pass is a string or a regex; 无论pass是字符串还是正则表达式都没有道理。 neither have a style property. 都没有style属性。 Putting the variable in parentheses has no effect. 将变量放在括号中无效。 It doesn't work in any browser for me, including IE. 对于我来说,它不适用于任何浏览器,包括IE。

I would guess you meant: 我猜你的意思是:

function showDiv(pass) {
    var divs= document.getElementsByTagName('span');    
    for (var i= 0; i<divs.length; i++)
        divs[i].style.visibility= divs[i].id==pass? 'visible' : 'hidden';
}

Note also the var i : this is necessary to stop i becoming a global variable. 另请注意var i :这是停止i成为全局变量所必需的。 It is a very common source of weird errors when two for i loops start interfering with each other. 当两个for i循环开始相互干扰时,这是一个非常常见的怪异错误源。

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

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