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

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

js通过DOM改变html和css

 

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>

js通过DOM改变html和css

 

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>

js通过DOM改变html和css

 

相关文章:

  • 2022-12-23
  • 2021-07-20
  • 2022-12-23
  • 2022-01-10
  • 2021-12-19
  • 2021-11-22
  • 2022-12-23
猜你喜欢
  • 2021-10-15
  • 2021-04-19
  • 2022-12-23
  • 2022-12-23
  • 2021-09-23
  • 2021-10-22
  • 2021-12-10
相关资源
相似解决方案