[英]Hide element with Javascript. Doesn't work with IE and Chrome
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 在 Firefox 上工作,但不是在 Chrome 和 IE6 中,但没有从那里得到帮助。 Javascript code, what should hide/show textbox and radio buttons: 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';
}
And HTML:和 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.现在使用 Firefox 和 Opera,当我选择其中一个选项时,它会将单选按钮和文本框更改为可见/隐藏。 But with Chrome and IE they wouldn't.但是对于 Chrome 和 IE,他们不会。 I also tried with "... .style.display = 'none';"我也试过“... .style.display = 'none';” nothing happened (on IE and Chrome, FF and Opera it works) even this time.即使这次也没有发生任何事情(在 IE 和 Chrome、FF 和 Opera 上都可以)。
FF 5.0;法郎 5.0; Chrome 12;铬 12; IE 8;即 8; Opera 11.50歌剧 11.50
Now works with:现在适用于:
<select onchange='hideshow(this.value);' id='listb2'>
<option value='1'>Age</option>
<option value='2'>Sex</option>
</select>
onclick
isn't valid for option
elements. onclick
对option
元素无效。
Handle the onchange
event of the select
instead, get the selected value using the code below and then do your conditional processing.改为处理select
的onchange
事件,使用下面的代码获取选定的值,然后进行条件处理。
var element = document.getElementById("listb2");
var selectedValue = element.options[element.selectedIndex].value;
Instead of setting 'visibility' to 'hidden', try setting 'display' to 'none'.不要将“可见性”设置为“隐藏”,而是尝试将“显示”设置为“无”。
document.getElementById('header').style.display = 'none';
EDIT: Not quite sure whether you can set the onclick
property on an option
element in those browsers... Try using onchange
on your surrounding select
tag instead.编辑:不太确定是否可以在这些浏览器中的option
元素上设置onclick
属性...尝试在周围的select
标记上使用onchange
。
I realy encourage you to use jQuery for that.我真的鼓励您为此使用 jQuery。 jQuery guarantees that this will work in every browser. jQuery 保证这将适用于每个浏览器。 In query its very easy在查询它很容易
<script>
$().ready(function () {
$("#listb2").change(function () {
if ($("#listb2").val() == "1")
$("#radio1, #radio2, #rlabel").show();
else
$("#text1, #radio1, #radio2, #rlabel").hide();
});
});
</script>
<select id='listb2'>
<option value='1'>Age</option>
<option value='2'>Sex</option>
</select>
If you remove the onclick events from the select box like this:如果您从 select 框中删除 onclick 事件,如下所示:
<select id='listb2'>
<option value='1'>Age</option>
<option value='2'>Sex</option>
</select>
Then you can bind an onChange event using javascript:然后您可以使用 javascript 绑定 onChange 事件:
var selectBox = document.getElementById("listb2");
selectBox.onchange = function(e){
if(selectBox.value == 1) {
// Do what you want for Age
hide1();
} else if(selectBox.value == 2) {
// Do what you want for Sex
show1();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.