天天看點

div擷取焦點的方法

div元素能實作擷取焦點的方法:

給div元素添加一個 tabindex屬性,這個屬性的取值範圍為≥0的整數。tabindex屬性值越小(最小為0)其所在的标簽越先得到焦點 

<style>
        div{
            width: 100px;
            height: 100px;
            outline:none;
            background-color: #f00;
        }
    </style>
</head>
<body>
  
    <div tabindex="1"></div>
    <script>
        var oDiv=document.querySelector("div")
        oDiv.onfocus=function(){
           oDiv.style.backgroundColor="blue"
        }
    </script>