开发者社区> 问答> 正文

为什么在IE中运行这段代码会提示type为空或不是对象?为什么border属性会无法移除?

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>example</title>
        <style type="text/css">
            p{
                border: thin solid black;
                background: gray;
                color: white;
                padding: 10px;
                margin: 5px;
            }
        </style>
    </head>
    <body>
        <p>
            <label for="fave">fruit:<input id="fave" name="name"/></label>
        </p>
        <p>
            <label for="name">name:<input id="name" name="name"/></label>
        </p>
        <button type="submit">submit vote</button>
        <button type="reset">reset</button>
        <script type="text/javascript" defer="true">
            function handleFocusEvent(e){
                if(e.type=="focus"){//为什么在IE中运行这段代码会提示type为空或不是对象?
                    e.target.style.backgroundColor="lightgray";
                    e.target.style.border="thick solid red";
                }else{
                    e.target.style.removeProperty("background-color");
                    e.target.style.border="thick solid black";
//              e.targer.style.removeProperty("border")//为什么border属性会无法移除?
                }
            }

            var inputElems=document.getElementsByTagName("input");
            for(var i=0;i<inputElems.length;i++){
                inputElems[i].onfocus=handleFocusEvent;
                inputElems[i].onblur=handleFocusEvent;
            }
        </script>
    </body>
</html>

展开
收起
小旋风柴进 2016-03-13 16:33:55 3449 0
1 条回答
写回答
取消 提交回答
  • 
            function handleFocusEvent(e) {
                e = e || window.event;//ie事件存储在window.event中,不是直接传入函数参数
                var o = e.srcElement || e.target;//ie源对象是srcElement,不是target,要做兼容
                if (e.type == "focus") {//为什么在IE中运行这段代码会提示type为空或不是对象?
                    o.style.backgroundColor = "lightgray";
                    o.style.border = "thick solid red";
                } else {
                    o.style.cssText = ''//IE8-不支持style的removeProperty,直接设置cssText清空所有style的内容或者自己替换下cssText对应的属性
                    o.style.border = "thick solid black";
                    //              e.targer.style.removeProperty("border")//为什么border属性会无法移除?
                }
            }
    2019-07-17 19:02:52
    赞同 展开评论 打赏
问答分类:
Go
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载