js DOM改变html和css

当网页被加载时,浏览器会创建页面的文档对象模型DOM,即Document Object Model。

1.改变html输出流,通过document.write() 直接向 HTML 输出流写内容

<body>
    <p>段落</p>
    <script>    
        document.write(Date());
    </script>    
</body>

2.改变html内容,document.getElementById(id).innerHTML=''新的html

<p id="p1">Hello World!</p>
<p id="p2">Hello World!</p>
<script>
    document.getElementById("p2").innerHTML="新文本!";
</script>

3.改变html属性,document.getElementById(id).attribute=新属性值

<img id="image" src="smiley.gif" width="160" height="120">
<script>
    document.getElementById("image").height='160';
</script>

会将图片调整为长、宽相等的正方形图片

4.改变css,通过document.getElementById("p1").style.css属性=css属性值

<p id="p1">Hello World!</p>
<p id="p2">Hello World!</p>
<script>
    document.getElementById("p1").style.color='yellow';
    document.getElementById("p2").style.background="red";
</script>

猜你喜欢

转载自www.cnblogs.com/Forever77/p/10268850.html