---
title: "javascript操作css"
author: "lijunabc"
date: "2010-06-09T13:32:00Z"
tags: ["CSS", "HTML", "JavaScript"]
categories: ["Web前端"]
slug: "javascript-css"
draft: false
lastmod: "2010-06-09T13:32:00Z"
id: 687930
url: "https://lijunabc.iteye.com/blog/687930"
---

&lt;html&gt;&lt;head&gt;&lt;title&gt;动态改变文字颜色 &lt;/title&gt;  
    &lt;style type="text/css"&gt;  
        body { background-color: pink;  
               font-size: 22pt;  
               font-weight: bold;  
        }  
        .red { color:rgb(255,0,0);   
               font-style: italic;  
               font-size: 32;  
        }  
        .blue { color:blue;  
                font-size: 36;  
        }  
        .green { color: green;  
                 font-size: 40;  
        }  
        .yellow { color: yellow;  
                 font-size: 45;  
        }         
    &lt;/style&gt;  
    &lt;script language="javascript"&gt;  
        function init(){  
            div1=document.getElementById("first");  
            div2=document.getElementById("second");  
            div3=document.getElementById("third");  
            div4=document.getElementById("fourth");  
        }  
        function colorText(){  
            div1.style.left = 50;  
            div1.style.top = 50;  
            div1.className="red";  
            div2.style.left = 100;  
            div2.style.top = 100;  
            div2.className="blue";  
            div3.style.left = 150;  
            div3.style.top = 150;  
            div3.className="green";  
            div4.style.left = 200;  
            div4.style.top = 200;  
            div4.className="yellow";  
        }  
    &lt;/script&gt;  
    &lt;/head&gt;  
    &lt;body onLoad="init()"&gt;  
        &lt;div id="first"  style="position:absolute; top:50px"&gt;戌鼓断人行，秋边一雁声。&lt;/div&gt;  
        &lt;div id="second"  style="position:absolute; top:100px"&gt;露从今夜白，月是故乡明。&lt;/div&gt;  
        &lt;div id="third"  style="position:absolute; top:150px"&gt;有弟皆分散，无家问死生。&lt;/div&gt;  
        &lt;div id="fourth"  style="position:absolute; top:200px"&gt;有弟皆分散，无家问死生。&lt;/div&gt;  
    &lt;form&gt;  
        &lt;input type="button" value="移动文字位置并改变样色" onClick="colorText()"&gt;  
    &lt;/form&gt;  
    &lt;/body&gt;