Javascript在Firefox上工作,但不是在Chrome和IE6上 [英] Javascript working on Firefox but not in Chrome and IE6
问题描述
我的JavaScript在Firefox 3.x.x中工作正常,但在IE *,Chrome,Safari中无法正常工作。调用函数之前的简单警报工作。这里是代码
function showDiv(div){
然后显示警告框,但是除了fire fox之外的其他浏览器中不调用该函数
// 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';
$ / code $ / pre
这里是html页面代码
< td align =center>< a onclick =showDiv('<?= $ val ['keyname']?>')风格= 光标:指针; >编辑< / A>< / TD>
如果我把
alert()
code> showDiv('<?= $ val ['keyname']?>')
请告诉我这个解决方案。
解决方案在你发布的代码中本质上是错误的。我建议你发布一个可重现的非工作示例:问题将在页面的其他地方。也许
div
ID字符串不是唯一的(这是无效的HTML并且会使行为不可靠);也许还有一些其他的脚本干扰,也许你有事件代码触发这不是用跨浏览器的方式写的
然而,你尝试以三种不同的方式隐藏一个元素像过度杀伤我。只需一个
显示
更改就可以了。
另一种方法是将
className ='hidden'
或''
,并使用CSS规则将该类映射到display:none
。这样做的好处是,您不必知道所涉及的元素是否是< div>
(应该还原为display:块
),a< span>
(应该返回到display:inline
)或者是其他东西。 (与表格相关的元素有特殊的问题。)I have javascript that working fine in Firefox 3.x.x, but it does not work in 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
<td align="center"><a onclick="showDiv('<?=$val['keyname']?>')" style="cursor:pointer;">Edit</a></td>
if I put
alert()
beforeshowDiv('<?=$val['keyname']?>')
then alert box is displayed but the function is not called in other browsers other than fire foxPlease tell me the solution for this.
解决方案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); maybe there's some other script interfering, maybe you have event code firing this that's not written in a cross-browser wayHowever your attempts to hide an element in three different ways seem like overkill to me. Just a single
display
change would do it fine.Another way to do it is to set
className='hidden'
or''
, and use a CSS rule to map that class todisplay: none
. The advantage of this is that you don't have to know whether the element in question is a<div>
(that should revert todisplay: block
), a<span>
(that should revert todisplay: inline
) or something else. (The table-related elements have particular problems.)这篇关于Javascript在Firefox上工作,但不是在Chrome和IE6上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!