您现在的位置:主页 > 网络编程 > XML编程 >

IE中input元素的.type为只读

来源:建站吧 责任编辑:Jzq8 发表时间:2011-10-18 点击:

今天遇到一处错误,发现IE中input元素的.type为只读!
 

相关形容:
ie下input元素的.type无法动态设置;
input.type=”hidden”;
IE中input无法在text和hidden间动态改变;

问题描述:
有这么一段:

<scrt type="text/javcript">
    <!--
    function $(){
        return document.ElementById?document.getElementById(arguments[0]):eval(arguments[0]);
    }
    //...
    function safeQSelOnChange()
    {
        if($('safeQuestionSel').value==-1)
        {
            var safeQuestion = $('safeQuestion');
            safeQuestion.input="text";
            safeQuestion.value="";
            safeQuestion.focus();
        }else{
            var safeQuestion = $('safeQuestion');
            var safeQuestionSel = $('safeQuestionSel');
            safeQuestion.input="hidden";
            safeQuestion.value=safeQuestionSel.options[safeQuestionSel.edIndex].inner;
        }
    }
    //-->
</script>
配合这么一段html:

<select class="inputLen" name="safeQuestionSel" id="safeQuestionSel" onchange=":safeQSelOnChange();" onblur="javascript:safeQSelOnChange();" >
      <option value="1">你爸爸妈妈都叫你什么小名?</option>
      <option value="2">你最要好的朋友叫什么?</option>
      <option value="3">你最尊敬的老师是?</option>
      <option value="-1" >自定义问题</option>
</select>
<br/>
<input class="inputLen"  type="hidden" name="safeQuestion" id="safeQuestion" size="28"  value="" />
本想实现:
允许用户在下拉列表中选择一项,若选中最后一项“自定义问题”,则文本框可见并设置其焦点;若选其他项,则文本框隐藏并被赋值为选中项的文本。
FF可以正常执行,在IE下却无法设置safeQuestion.type=”text”

原因:
FF下支持input元素.type [ = sType ],IE下input.type属性却是只读的.
官方说法: 在IE中,除了使用createElement方法动态创建的元素,其他情况下,type属性都为只读

解决方案:
改成Style控制显示与否咯…

<script type="text/javascript">
    <!--
    function $(){
        return document.getElementById?document.getElementById(arguments[0]):eval(arguments[0]);
    }
    function safeQSelOnChange()
    {
        if($('safeQuestionSel').value==-1)
        {
            var safeQuestion = $('safeQuestion');
            safeQuestion.style.display = 'block';
            safeQuestion.value="";
            safeQuestion.focus();
        }else{
            var safeQuestion = $('safeQuestion');
            var safeQuestionSel = $('safeQuestionSel');
            safeQuestion.style.display = 'none';
            safeQuestion.value=safeQuestionSel.options[safeQuestionSel.selectedIndex].innerHTML;
        }
    }
    //-->
</script>
<select class="inputLen" name="safeQuestionSel" id="safeQuestionSel" onchange="javascript:safeQSelOnChange();" onblur="javascript:safeQSelOnChange();" >
      <option value="1">你爸爸妈妈都叫你什么小名?</option>
      <option value="2">你最要好的朋友叫什么?</option>
      <option value="3">你最尊敬的老师是?</option>
      <option value="-1" >自定义问题</option>
</select>
<br/>
<input class="inputLen" style="display:none;margin:3px 0 3px 0 " type="text" name="safeQuestion" id="safeQuestion" size="28"  value="" />

    发表评论
    请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
    评价:
    表情:
    验证码:点击我更换图片

    今日头条

    更多>>

    推荐图文

    • XML和实体序列化和反序列化
    • XML数据读取方式性能比较(一)
    • 给XML初学者的教程
    • 解析XML (很给力)
    • XML轻松学习手册(2)XML概念
    • XML文件的生成基本方式(代码+注释)
    • 读取XML为行记录
    • 如何使用XML实现多渠道接入网站的构架
    • JSON是什么?它能带来什么?
    Alexa - 客户服务 - 联系方法 - 招聘信息 - 友情链接 - 网站地图 - TAG标签 - RSS订阅
    Copyright © 2010-2012 JZQ8.COM. 建站吧|建站去吧 版权所有
    冀ICP备09002514号
    冀ICP备09002514号 网络报警 企业法人营业执照 中国互联网协会 支付宝付款 网银在线付款