亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

關于獲取元素樣式的兼容性寫法為什么不行呢?

關于獲取元素樣式的兼容性寫法為什么不行呢?

翻閱古今 2018-12-20 18:15:42
<!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>無標題文檔</title><style>div { width:100px; height:120px; background:red; }</style><!--<script src="miaov.js"></script>--><script>function $( v ){    if( typeof v === 'function' ){         return window.onload = v;        //console.log(v)    } else if ( typeof v === 'string' ) {        return document.getElementById(v);    } else if ( typeof v === 'object' ) {        return v;    }}$(function(){        // $('div1').style.width = '300px';            $('btn1').onclick = function  (){                //var aaa=getComputedStyle( $('div1')).width || $('div1').currentStyle.width        var aaa=  $('div1').currentStyle.width ||  getComputedStyle( $('div1')).width        ***//在這里用申明變量的方式寫了ie和chrome的獲取元素樣式的寫法,為什么會報錯的?備注:這里用的$不是jquery,而是上面定義的$函數***        alert(aaa)            };});</script></head><body><input id="btn1" type="button" value="按鈕" /><div id="div1"></div></body></html>
查看完整描述

1 回答

?
翻過高山走不出你

TA貢獻1875條經驗 獲得超3個贊

你思路是沒啥問題,但是細節有點問題。


https://img1.sycdn.imooc.com//5c4024b60001998905820023.jpg

你是想||運算符,返回其中一個為真的,但是它只能用來處理真假,而錯誤不能算是假。

$('div1').currentStyle 返回的是一個undefined,你再取他的屬性,自然是報錯。||不能判斷錯誤。

你可以先判斷,再取width的值。


 var aaa=  $('div1').currentStyle ||  getComputedStyle( $('div1'));

 var aaaa = aaa.width;


查看完整回答
反對 回復 2019-01-17
  • 1 回答
  • 0 關注
  • 468 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號