亚洲最大看欧美片,亚洲图揄拍自拍另类图片,欧美精品v国产精品v呦,日本在线精品视频免费

  • 站長(zhǎng)資訊網(wǎng)
    最全最豐富的資訊網(wǎng)站

    javascript怎么設(shè)置元素寬度

    javascript設(shè)置元素寬度的方法:首先使用“document.getElementById("id值")”語句獲取元素對(duì)象;然后使用“元素對(duì)象.style.width="寬度值"”語句來設(shè)置元素的寬度。

    javascript怎么設(shè)置元素寬度

    本教程操作環(huán)境:windows7系統(tǒng)、javascript1.8.5版、Dell G3電腦。

    javascript設(shè)置元素寬度

    原理:

    • 獲取元素對(duì)象

    • HTML DOM Style 對(duì)象的width 屬性設(shè)置元素的寬度。

    代碼示例:

    <!DOCTYPE html> <html>  	<head> 		<meta charset="UTF-8"> 		<style> 			img { 				width: 200px; 			} 		</style> 	</head>  	<body> <img id="img" src="img/1.jpg" />  <p id="demo">img圖片寬度為200px。</p>  <button onclick="myFunction()">設(shè)置img圖片的寬度</button>  <script> 	function myFunction() { 		document.getElementById("img").style.width="300px"; 		document.getElementById("demo").innerHTML="img圖片寬度為300px。"; 	} </script> 	</body>  </html>

    效果圖:

    javascript怎么設(shè)置元素寬度

    說明:

    Style 對(duì)象的width 屬性可以設(shè)置元素的寬度。

    語法:

    Object.style.width=auto|length|%
    描述
    auto 默認(rèn)。瀏覽器可計(jì)算出實(shí)際的寬度。
    % 定義基于其包含塊的百分比寬度。
    length 使用 px、cm 等單位定義寬度。

    查找元素的方法:

    1.根據(jù)id獲取元素

    document.getElementById("id屬性的值");

    2.根據(jù)標(biāo)簽名字獲取元素

    document.getElementsByTagName("標(biāo)簽的名字");

    3.根據(jù)name屬性的值獲取元素

    document.getElementsByName("name屬性的值");

    4.根據(jù)class屬性獲取元素

    document.getElementsByClassName("類樣式的名字");

    5.根據(jù)css路徑獲取元素 (獲取一個(gè))

    document.querySelector("css路徑");

    6.根據(jù)css路徑獲取元素 (獲取一組)

    document.querySelectorAll("css路徑");

    【推薦學(xué)習(xí):javascript高級(jí)教程】

    贊(0)
    分享到: 更多 (0)
    網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)