JavaScript第一講之js操作css

來源:互聯網
上載者:User

標籤:尋找   調用   set   ack   ntb   一個   orange   height   body   

原本應該是由css進行控制html中的div的寬高和背景顏色,但是在下方使用了JavaScript進行重新調用了div盒子,並且覆蓋了css原本的屬性內容。

需求目標:由 100 像素的粉色背景色的div盒子變成了 300像素的橘黃色效果的div盒子

代碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>js控制css</title><style>    <!-- div是html盒子的元素選取器 -->div{    background:pink;     <!-- 背景色:粉色  -->    width:100px;        <!-- 寬度:100像素-->    height:100px;        <!-- 高度:100像素 -->}</style></head><body><div id="txt"></div></body></html>
<!-- JavaScript代碼只能在下面的script的標籤體中可以書寫 --><script>//方式一: //尋找到id為div的標籤之後 繼續尋找style屬性 // document.getElementById(‘txt‘).style.width=‘300px‘; // document.getElementById(‘txt‘).style.height=‘300px‘; // document.getElementById(‘txt‘).style.background=‘orange‘; //方式二: //先找到 id 為txt 的元素,把它放到一個變數中,這個過程可以理解為將右側的結果賦值給左側 var t = document.getElementById(‘txt‘); // t就是變數名稱,代表著html的盒子所在位置 // 將盒子的樣式中的寬度設定為300像素 t.style.width=‘300px‘; // 將盒子的樣式中的高度設定為300像素 t.style.height=‘300px‘; // 將盒子的樣式中的背景色設定橘黃色 t.style.background=‘orange‘;</script>

運行後的如下:

JavaScript第一講之js操作css

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.