使用 Javascript 隐藏元素。不适用于 IE 和 Chrome
我在隐藏元素时遇到问题。 我已经读过这个主题 Javascript 可以在 Firefox 上运行,但不能在 Chrome 和 IE6 中运行 但没有从那里得到帮助。 Javascript 代码,什么应该隐藏/显示文本框和单选按钮:
function hide1(a)
{
var text1=document.getElementById("text1");
text1.style.visibility = 'visible';
text1.value = a;
document.getElementById("radio1").style.visibility = 'hidden';
document.getElementById("radio2").style.visibility = 'hidden';
document.getElementById("rlabel").style.visibility = 'hidden';
}
function show1()
{
document.getElementById("text1").style.visibility = 'hidden';
document.getElementById("radio1").style.visibility = 'visible';
document.getElementById("radio2").style.visibility = 'visible';
document.getElementById("rlabel").style.visibility = 'visible';
}
和 HTML:
<select id='listb2'>
<option onclick='hide1("$age");' value='1'>Age</option>
<option onclick='show1();' value='2'>Sex</option>
</select>
现在,当我选择其中一个选项时,Firefox 和 Opera 会将单选按钮和文本框更改为可见/隐藏。但对于 Chrome 和 IE,他们就不会了。我也尝试过“....style.display = 'none';”即使这次也没有发生任何事情(在 IE 和 Chrome、FF 和 Opera 上都可以)。
FF 5.0;铬12; IE 8; Opera 11.50
现在适用于:
<select onchange='hideshow(this.value);' id='listb2'>
<option value='1'>Age</option>
<option value='2'>Sex</option>
</select>
I have problems with hiding elements.
I already read this topic
Javascript working on Firefox but not in Chrome and IE6
but didn't get help from there. Javascript code, what should hide/show textbox and radio buttons:
function hide1(a)
{
var text1=document.getElementById("text1");
text1.style.visibility = 'visible';
text1.value = a;
document.getElementById("radio1").style.visibility = 'hidden';
document.getElementById("radio2").style.visibility = 'hidden';
document.getElementById("rlabel").style.visibility = 'hidden';
}
function show1()
{
document.getElementById("text1").style.visibility = 'hidden';
document.getElementById("radio1").style.visibility = 'visible';
document.getElementById("radio2").style.visibility = 'visible';
document.getElementById("rlabel").style.visibility = 'visible';
}
And HTML:
<select id='listb2'>
<option onclick='hide1("$age");' value='1'>Age</option>
<option onclick='show1();' value='2'>Sex</option>
</select>
Now with Firefox and Opera it change radio buttons and textbox to visible/hidden when I choose one of the options. But with Chrome and IE they wouldn't. I also tried with "... .style.display = 'none';" nothing happened (on IE and Chrome, FF and Opera it works) even this time.
FF 5.0; Chrome 12; IE 8; Opera 11.50
Now works with:
<select onchange='hideshow(this.value);' id='listb2'>
<option value='1'>Age</option>
<option value='2'>Sex</option>
</select>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
onclick
对于option
元素无效。相反,处理
select
的onchange
事件,使用下面的代码获取选定的值,然后进行条件处理。onclick
isn't valid foroption
elements.Handle the
onchange
event of theselect
instead, get the selected value using the code below and then do your conditional processing.不要将“可见性”设置为“隐藏”,而是尝试将“显示”设置为“无”。
编辑:不太确定是否可以在这些浏览器中的
option
元素上设置onclick
属性...尝试在周围的onchange
上使用onchange
code>select 标签代替。Instead of setting 'visibility' to 'hidden', try setting 'display' to 'none'.
EDIT: Not quite sure whether you can set the
onclick
property on anoption
element in those browsers... Try usingonchange
on your surroundingselect
tag instead.我真的鼓励您为此使用 jQuery。 jQuery 保证这适用于所有浏览器。查询起来很简单
I realy encourage you to use jQuery for that. jQuery guarantees that this will work in every browser. In query its very easy
如果您从选择框中删除 onclick 事件,如下所示:
然后您可以使用 javascript 绑定 onChange 事件:
If you remove the onclick events from the select box like this:
Then you can bind an onChange event using javascript: