第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定

為什么把margin-left寫在css里就不能用ball.style.marginLeft了?

<!doctype html>
<html lang="en">
<head>
? ?<meta charset="UTF-8">
? ?<title>Promise</title>
? ?<style>
? ? ? ?.ball{width:40px;height: 40px;border-radius: 50%; margin-left: 0px;}
? ? ? ?.ball1{background: red;margin-left: 0px;}
? ? ? ?.ball2{background: green;}
? ? ? ?.ball3{background: blue;}
? ?</style>
</head>
<body>
? ?<div class="ball ball1" ></div>
? ?<div class="ball ball2" ></div>
? ?<div class="ball ball3" ></div>

<script>
? ?var ball1 = document.querySelector('.ball1');
? ?var ball2 = document.querySelector('.ball2');
? ?var ball2 = document.querySelector('.ball3');

? ?function ani(ball,distance,cb){
? ? ? ?setTimeout(function(){
? ? ? ? ? ?var marle = parseInt(ball.style.marginLeft,10);
? ? ? ? ? ?if(marle === distance){
? ? ? ? ? ? ? ?cb && cb();
? ? ? ? ? ?}else{
? ? ? ? ? ? ? ?if(marle < distance){
? ? ? ? ? ? ? ? ? ?marle ++ ;
? ? ? ? ? ? ? ?}else{
? ? ? ? ? ? ? ? ? ?marle -- ;
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?ball.style.marginLeft = marle + "px";
? ? ? ? ? ? ? ?ani(ball,distance,cb)
? ? ? ? ? ?}
? ? ? ?},13)
? ?}

? ?//ani(ball1,100,function(){})

</script>
</body>
</html>



如果 這樣寫 ?而不是寫行內(nèi) ? ?就無法用var marle = parseInt(ball.style.marginLeft,10); ? 這是為什么

正在回答

2 回答

用style獲取css樣式,只能獲取到寫到標(biāo)簽內(nèi)的樣式。參考網(wǎng)址:http://www.cnblogs.com/liulin0524/p/5315908.html

0 回復(fù) 有任何疑惑可以回復(fù)我~

js里獲取style里的樣式屬性不同于直接獲取dom節(jié)點(diǎn)里的屬性

可借鑒

function GetCurrentStyle (obj, prop) {?????
??? if (obj.currentStyle) {????????
??????? return obj.currentStyle[prop];?????
??? }??????
??? else if (window.getComputedStyle) {????????
??????? propprop = prop.replace (/([A-Z])/g, “-$1″);???????????
??????? propprop = prop.toLowerCase ();????????
??????? return document.defaultView.getComputedStyle (obj,null)[prop];?????
??? }??????
??? return null;???
} ?


alert(GetCurrentStyle(dd,”width”)); ???

0 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消
進(jìn)擊Node.js基礎(chǔ)(二)
  • 參與學(xué)習(xí)       76727    人
  • 解答問題       242    個(gè)

本教程帶你攻破 Nodejs,讓 JavaScript流暢運(yùn)行在服務(wù)器端

進(jìn)入課程

為什么把margin-left寫在css里就不能用ball.style.marginLeft了?

我要回答 關(guān)注問題
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)