4

別の CSS ファイルに記述されている要素の背景スタイルを取得しようとしています。問題は、CSS ファイルに記述されたスタイルを取得できないことです。

一方、HTML文書に書かれたスタイリングは取得可能です。

CSSコード

#try2
{ 
    background-color:yellow;
}
body
{
    background-color:gray;
}
td, th{
    border: 1px solid black;
}

HTMLコード

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=windows-1255" />
        <link rel="stylesheet" type="text/css" href="html.css">
        <script type="text/javascript" src="external.js"></script>
    </head>
    <body>
        <table>
            <tr>
                <td id = "try1" style="background-color:green;"><p id="ChosenColor3"> htmlfile</p></td>
            </tr>
            <tr>
                <td id = "try2"><p id="ChosenColor4"> css file</p></td>
                <td><button id="bestRated3" onclick = arrayTest()> ב.מ </button></td>
                <td><button id="submitForm" onclick = submit()> end</button></td>
            </tr>
            <tr>
                <td><h1 id="ChosenColor5"> text </h1></td>  
            </tr>
        </table>
    </body>
    <script>
        window.onload=aaa();
        function aaa()
        {
            var x = document.getElementById("try2");
            alert(x.style.background);
        }
    </script>     
</html>

ご覧のとおり、受け取ったメッセージは空です。IDを「try1」に変更すると表示されます。

4

5 に答える 5

1

これを試して

   function aaa()
        {
           var a = document.getElementById("try2").style.backgroundcolor;
           alert("Your background color is :"+a);
        }
于 2013-09-04T08:24:08.803 に答える
1

アラートを次のように変更しますalert(x.style.backgroundColor);

style.backgroundstyle.backgroundColorは 2 つの異なるものです。

于 2013-09-04T08:17:33.563 に答える
0

代わりにこれを使用してください:

alert(x.style.backgroundColor);
于 2013-09-04T08:17:45.157 に答える