久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

javascript - 封裝函數,實現游覽器兼容。

瀏覽:140日期:2023-05-19 18:10:14

問題描述

<!doctype html><html lang='en'><head> <meta charset='UTF-8'> <title>封裝獲取css屬性</title> <style>h1{ width:300px; height:100px; background-color:red;} </style></head><body> <h1>我是高100px,寬300px的紅色長方形</h1> <script type='text/javascript'>var h1 = document.getElementsByTagName(’h1’)[0];//標準瀏覽器console.log( window.getComputedStyle(h1)[’width’]);console.log( window.getComputedStyle(h1).height);//IE瀏覽器console.log(h1.currentStyle.width);console.log(h1.currentStyle.height);//封裝函數//ele表示元素,zxc表示屬性functiong abc (ele,zxc){ //第一種 var qwe = window.getComputedStyle( ele ).zxc||ele.current.zxc console.log(qwe); //第二種 if (window.getComputedStyle( ele )){window.getComputedStyle( ele ).zxc }else{ele.current.zxc }} </script></body></html>

這樣封裝對不對?

問題解答

回答1:

你要先判斷函數存不存在

function getStyle(elem, attr) { let style; if (window.getComputedStyle) { // 標準// 防止 elem === documentlet view = (elem.ownerDocument || elem).defaultView;if (!view || !view.opener) { view = window;}style = window.getComputedStyle(elem)[attr]; } else if (document.documentElement.currentStyle) { // IEstyle = elem.currentStyle[attr]; } elem = null; return style;}

標簽: JavaScript
主站蜘蛛池模板: 扶余县| 民丰县| 苍山县| 大新县| 禹州市| 登封市| 富源县| 那曲县| 潍坊市| 芜湖县| 壶关县| 温州市| 彭阳县| 垫江县| 永修县| 中西区| 通州市| 金阳县| 西城区| 平原县| 新蔡县| 绵阳市| 石嘴山市| 建昌县| 汉川市| 弥渡县| 黎川县| 黔东| 广平县| 桃园县| 上饶县| 金山区| 南康市| 涞水县| 商城县| 怀来县| 永德县| 常熟市| 措美县| 政和县| 新晃|