在所有浏览器中使用javascript显示或隐藏元素

时间:2013-01-08 05:39:25

标签: javascript html css

这是html代码..

<select id="dept" name="dept" style="float: left;">
        <option value="eee" onclick="toggleDisplay('dept_input_div', 0)">EEE</option>
        <option value="ece" onclick="toggleDisplay('dept_input_div', 0)">ECE</option>
        <option value="eie" onclick="toggleDisplay('dept_input_div', 0)">EIE</option>
        <option value="cse" onclick="toggleDisplay('dept_input_div', 0)">CSE</option>
        <option value="it" onclick="toggleDisplay('dept_input_div', 0)">IT</option>
        <option value="mechanical" onclick="toggleDisplay('dept_input_div', 0)">Mechanical</option>
        <option value="civil" onclick="toggleDisplay('dept_input_div', 0)">Civil</option>
        <option value="mechatronics" onclick="toggleDisplay('dept_input_div', 0)">Mechatronics</option>
        <option value="biotech" onclick="toggleDisplay('dept_input_div', 0)">Bio-tech</option>
        <option value="automobile" onclick="toggleDisplay('dept_input_div', 0)">Automobile</option>
        <option value="chemical" onclick="toggleDisplay('dept_input_div', 0)">Chemical</option>
        <option value="aeronautical" onclick="toggleDisplay('dept_input_div', 0)">Aeronautical</option>
        <option value="architecture" onclick="toggleDisplay('dept_input_div', 0)">Architecture</option>
        <option value="others" onclick="javascript:toggleDisplay('dept_input_div', 1);">Others..</option>
      </select>
      <div id="dept_input_div" style="display: none">
      <input class="inputbox" name="dept-input" type="text" id="dept-inputbox" />
      </div>

这是javascript代码..

    function toggleDisplay(id, displayvalue)
{
    if ( displayvalue == 1 ) {
        document.getElementById(id).style.display = 'block';
    } else if ( displayvalue == 0 ) {
        document.getElementById(id).style.display = 'none';
    }
}

上面的代码在firefox中工作正常,即(兼容性视图已关闭)但在chrome和ie(兼容性已打开)中。

我做错了什么?有没有一种适用于所有浏览器的正确方法?

1 个答案:

答案 0 :(得分:2)

使用onchange而不是onclick

HTML

<select id="dept" name="dept" style="float: left;" onchange="toggleDisplay('dept_input_div' , this.value)">
            <option value="eee" >EEE</option>
            <option value="ece" >ECE</option>
            <option value="eie" >EIE</option>
            <option value="cse" >CSE</option>
            <option value="it" >IT</option>
            <option value="mechanical" >Mechanical</option>
            <option value="civil" >Civil</option>
            <option value="mechatronics" >Mechatronics</option>
            <option value="biotech" >Bio-tech</option>
            <option value="automobile" >Automobile</option>
            <option value="chemical" >Chemical</option>
            <option value="aeronautical" >Aeronautical</option>
            <option value="architecture" >Architecture</option>
            <option value="others">Others..</option>
</select>
<div id="dept_input_div" style="display: none">
      <input class="inputbox" name="dept-input" type="text" id="dept-inputbox" />
</div>

<强>的javascript

function toggleDisplay(id, displayvalue)
{    
    if ( displayvalue == "others" ) {
         document.getElementById(id).style.display = 'block';
    } else {
         document.getElementById(id).style.display = 'none';
    }
}