国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

javascript - 封裝函數(shù),實(shí)現(xiàn)游覽器兼容。

瀏覽:139日期: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];//標(biāo)準(zhǔn)瀏覽器console.log( window.getComputedStyle(h1)[’width’]);console.log( window.getComputedStyle(h1).height);//IE瀏覽器console.log(h1.currentStyle.width);console.log(h1.currentStyle.height);//封裝函數(shù)//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>

這樣封裝對(duì)不對(duì)?

問題解答

回答1:

你要先判斷函數(shù)存不存在

function getStyle(elem, attr) { let style; if (window.getComputedStyle) { // 標(biāo)準(zhǔn)// 防止 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;}

標(biāo)簽: JavaScript
主站蜘蛛池模板: 平度市| 凤阳县| 荆门市| 绥棱县| 门头沟区| 忻州市| 连平县| 嫩江县| 东兴市| 寻甸| 昌图县| 辉南县| 卢湾区| 新郑市| 晋江市| 忻州市| 淅川县| 唐海县| 金昌市| 南汇区| 乌鲁木齐市| 长沙县| 永平县| 琼结县| 长寿区| 庆阳市| 沛县| 磐石市| 平原县| 佛冈县| 浦城县| 家居| 凌海市| 平湖市| 邵阳县| 周口市| 阳高县| 佛坪县| 宜春市| 吐鲁番市| 汉寿县|