Javascript可以使用getElementsById setAttribute來做到跟改html元素屬性方法如下:

範例一

<img id="images" src="/" width="100"> 
<scritp> 
    document.getElementsById("images").setAttribute("width", "200"); 
<script>

 //執行結果 <img id="images" src="/" width="200">

範例二

<!DOCTYPE html>
<html>
<head>
<style>
    .democlass {
        color: red;
    }
</style>
</head>
<body>

<h1>Hello World</h1>

<p>Click the button to add a class attribute with the value of "democlass" to the h1 element.</p>

<button onclick="myFunction()">Try it</button>

<script>
    function myFunction() {
        document.getElementsByTagName("H1")[0].setAttribute("class", "democlass");
    }
</script>

</body>
</html>

相關連結展示:http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_element_setattribute1

您也可能喜歡這些文章

Copyright © 2018 ucamc