简体   繁体   English

Javascript可在Firefox上运行,但不能在Chrome和IE6中运行

[英]Javascript working on Firefox but not in Chrome and IE6

I have javascript that working fine in Firefox 3.xx, but it does not work in IE*, Chrome, Safari. 我的javascript在Firefox 3.xx中工作正常,但在IE *,Chrome,Safari中不起作用。 Simple alert work before calling function. 调用函数之前简单的警报工作。 Here is the code 这是代码

function showDiv(div){
 //alert(div);
 document.getElementById(div).style.visibility='visible';
 document.getElementById(div).style.height='auto';
 document.getElementById(div).style.display='block';}
function hideDiv(div){
 //alert(div);
 document.getElementById(div).style.visibility='hidden';
 document.getElementById(div).style.height='0px';
 document.getElementById(div).style.display='none';
}

here is the html page code 这是HTML页面代码

<td align="center"><a onclick="showDiv('<?=$val['keyname']?>')" style="cursor:pointer;">Edit</a></td>

if I put alert() before showDiv('<?=$val['keyname']?>') then alert box is displayed but the function is not called in other browsers other than fire fox 如果我将alert()放在showDiv('<?=$val['keyname']?>')则会显示警报框,但除showDiv('<?=$val['keyname']?>')浏览器外不会在其他浏览器中调用该函数

Please tell me the solution for this. 请告诉我解决方案。

The syntax looks okay to me. 语法对我来说还不错。

Make sure there are not multiple elements with the same ID in the document and that your element IDs are valid. 确保文档中没有多个具有相同ID的元素,并且您的元素ID有效。

There is nothing inherently wrong in the code you have posted. 您发布的代码没有内在的错误。 I suggest you post a reproduceable non-working example: the problem will be elsewhere in the page. 我建议您发布一个无法复制的示例:问题将出现在页面的其他地方。 Maybe the div ID string isn't unique (this is invalid HTML and will make behaviour unreliable); 也许div ID字符串不是唯一的(这是无效的HTML,将使行为不可靠); maybe there's some other script interfering, maybe you have event code firing this that's not written in a cross-browser way 可能还有其他脚本干扰,也许您触发了不是以跨浏览器方式编写的事件代码

However your attempts to hide an element in three different ways seem like overkill to me. 但是,您以三种不同方式隐藏元素的尝试对我来说似乎太过分了。 Just a single display change would do it fine. 只需更改一个display就可以了。

Another way to do it is to set className='hidden' or '' , and use a CSS rule to map that class to display: none . 另一种方法是设置className='hidden''' ,然后使用CSS规则将该类映射为display: none The advantage of this is that you don't have to know whether the element in question is a <div> (that should revert to display: block ), a <span> (that should revert to display: inline ) or something else. 这样做的好处是您不必知道所讨论的元素是<div> (应还原为display: block ), <span> (应还原为display: inline )还是其他。 (The table-related elements have particular problems.) (与表相关的元素有特定的问题。)

Maybe you could try that: 也许您可以尝试:

function showDiv(div) {
    var obj = document.getElementById(div);
    if (obj) {
        obj.style.display = "block";
        obj.style.height = "auto";
    } else {
       alert("DIV with id " + div + " not found. Can't show it.");
    }
}

function hideDiv(div) {
    var obj = document.getElementById(div);
    if (obj) {
        obj.style.display = "none";
    } else {
       alert("DIV with id " + div + " not found. Can't hide it.");
    }
}

Do not call document.getElementById several times in the same function, use a variable to store the div element. 不要在同一函数中多次调用document.getElementById ,请使用变量存储div元素。

The if (obj) test will only execute the code if it has been found by document.getElementById(...) . if (obj)测试仅由document.getElementById(...)找到,则仅执行代码。

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

相关问题 parent.location在Firefox和Chrome中不起作用,但在IE6中起作用 - parent.location is not working in Firefox and Chrome but working in IE6 Javascript在chrome中工作,而不是firefox或ie - Javascript working in chrome, not firefox or ie Javascript可在Firefox中运行,但不能在IE或Chrome中运行 - Javascript working in firefox but not IE or chrome Facebook JavaScript无法在IE上运行,但可以在Firefox和Chrome上运行 - Facebook JavaScript not working on IE, but working on Firefox and Chrome JavaScript适用于IE和Edge,但不适用于Chrome和Firefox - JavaScript working on IE and Edge but not working on Chrome and Firefox javascript(jQuery)在IE6中不起作用 - javascript (jQuery) not working in IE6 Javascript addEventListener可在Firefox和IE中运行,但不能在Chrome中运行吗? - Javascript addEventListener working in Firefox and IE but not Chrome? JavaScript无法在IE和Firefox中运行,在Chrome中运行良好 - JavaScript not working in IE and Firefox, works fine in Chrome JavaScript 在 Firefox、Chrome 中不起作用 - 在 IE、Edge 中有效 - JavaScript not working in Firefox, Chrome - works in IE, Edge Javascript代码在Chrome和firefox中不起作用,但在IE中起作用 - Javascript Code is not working in Chrome and firefox but works in IE
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM