[英]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.