ホームページ  >  記事  >  ウェブフロントエンド  >  jsを使ってCSSのスタイルを変更する方法

jsを使ってCSSのスタイルを変更する方法

青灯夜游
青灯夜游オリジナル
2021-05-20 17:27:1426265ブラウズ

方法: 1. "Object.style.Attribute name="value"" を使用します。 2. "Object.style.cssText="Attribute name:Value"" を使用します。 3. "Object.setAttribute( "class","クラス名")"; 4. setAttribute() 関数を使用して css ファイルを変更します。

jsを使ってCSSのスタイルを変更する方法

このチュートリアルの動作環境: Windows7 システム、JavaScript バージョン 1.8.5、Dell G3 コンピューター。

CSS スタイルを変更する Javascript メソッド (4 種類)

最初のメソッド: object.style.property name="value" を使用して、スタイルシートのクラス名を変更します。

例:

div1.style.width="100px";

2 番目: Object.style.cssText="属性名: 値" を使用して、埋め込み CSS を変更します。

例:

div1.style.cssText="width:100px;height:100px;background: palevioletred;";

3 番目の方法: object.setAttribute("class","class name") を使用して、スタイル シートのクラス名を変更します。

例:

div1.setAttribute("class","div2")

4 番目の方法: setAttribute() 関数を使用して外部 CSS ファイルを変更し、それによって要素の CSS を変更します。

例:

div1.setAttribute("href","css2.css");

html コード:

1d92bf69482700fca057d30f08918a96
c8f1bd2f7739fc4bad8f55bac472bd1f116b28748ea4df4d9c2150843fecfba68
6fd9c25e533a9d166641cb87d2448bc6216b28748ea4df4d9c2150843fecfba68
4dd653285949af25c14b463618444190316b28748ea4df4d9c2150843fecfba68
dd2751f7d31c01d83ceca356d9e60c77416b28748ea4df4d9c2150843fecfba68

css1.css ファイル

@charset "utf-8";
#divBtn1,#divBtn2,#divBtn3,#divBtn4{
    width:100px;
    height:100px;
    margin-bottom: 10px;
}
#divBtn1{background:yellowgreen;}
#divBtn2{background:paleturquoise;}
#divBtn3{border:1px solid #ccc}
#divBtn4{background:blue;}
.div3{width:100px;height:100px;background:blueviolet}

css2.css ファイル

@charset "utf-8";
#divBtn4{background: greenyellow;}
#divBtn1,#divBtn2,#divBtn3,#divBtn4{
    width:200px;
    height:200px;
    border:1px solid #ccc;
    margin-bottom: 10px;
}
#divBtn1{background:yellowgreen;}
#divBtn2{background:paleturquoise;}
.div3{width:100px;height:100px;background:blueviolet}

js コード:

<script>
            /*
             *javascript动态修改css效果的方法(四种) 
             * 第一种:div1.style.width=&quot;100px&quot;;
             * 第二种:div2.style.cssText="width:100px;height:100px;background: palevioletred;";
             * 第三种:div1.setAttribute(&quot;class&quot;,&quot;div2&quot;)和div3.className="div3";//效果一样
             * 第四种:使用更改外联的css文件,从而改变元素的css
             * obj.setAttribute("href","css/css2.css");
             * */
            function changeCss1(){
                var div1=document.getElementById("divBtn1");
                div1.style.width=&quot;100px&quot;;
                div1.style.height="100px";
                div1.style.background="red";
            }
            function changeCss2(){
                var div2=document.getElementById("divBtn2");
                div2.style.cssText="width:100px;height:100px;background: palevioletred;";                //cssText会覆盖之前的设置  无兼容性问题  写法和css样式表相同
            }
            function changeCss3(){
                var div3=document.getElementById("divBtn3");                //div3.className="div3";//效果一样
                div3.setAttribute("class","div3");
            }
            function changeCss4(){
                var obj=document.getElementById("cssLink");
                obj.setAttribute("href","css/css2.css");
            }
        </script>

プログラミング関連の知識については、プログラミング ビデオをご覧ください。 !

以上がjsを使ってCSSのスタイルを変更する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。