DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> Javascript實例代碼:點擊設置網頁背景
Javascript實例代碼:點擊設置網頁背景
編輯:關於JavaScript     

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>【javascript每日一練】- 根據選項設置背景顏色</title>
        <script type="text/javascript">
        function setProperty()
                {
                        var content = document.getElementById("content");
                        var sel = document.getElementById("selectColor");
                        var propertyValue = sel.options[sel.selectedIndex].value;
                        content.style["background"] = propertyValue;
                }
        </script>
</head>

<body>
        <div id="content">
                <p>選擇顏色後點擊按鈕"設置背景顏色"。</p>
                <select id="selectColor" >
                        <option value="aqua">aqua</option>
                        <option value="black">black</option>
                        <option value="blue">blue</option>
                        <option value="fuchsia">fuchsia</option>
                        <option value="gray">gray</option>
                        <option value="green">green</option>
                        <option value="lime">lime</option>
                        <option value="maroon">maroon</option>
                        <option value="navy">navy</option>
                        <option value="olive">olive</option>
                        <option value="purple">purple</option>
                        <option value="red">red</option>
                        <option value="silver">silver</option>
                        <option value="teal">teal</option>
                        <option value="white">white</option>
                        <option value="yellow">yellow</option>
                </select>
                <button onclick="setProperty(); return false;">設置背景顏色</button>
                <br />
                <br />
        </div>

</body>
</html>

今天寫這個浪費了我很多時間,本來是很簡單的一個小代碼可我就是寫不出效果來了。最後在百般無奈下我尋求sunylost大神的幫助,一語中的。
奉上之前錯誤代碼,望各位避免。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>【javascript每日一練】- 根據選項設置背景顏色</title>
        <script type="text/javascript">
        var content = document.getElementById("content");
        function setProperty()
                {
                        var sel = document.getElementById("selectColor");
                        var propertyValue = sel.options[sel.selectedIndex].value;
                        content.style["background"] = propertyValue;
                }
        </script>
</head>

<body>
        <div id="content">
                <p>選擇顏色後點擊按鈕"設置背景顏色"。</p>
                <select id="selectColor" >
                        <option value="aqua">aqua</option>
                        <option value="black">black</option>
                        <option value="blue">blue</option>
                        <option value="fuchsia">fuchsia</option>
                        <option value="gray">gray</option>
                        <option value="green">green</option>
                        <option value="lime">lime</option>
                        <option value="maroon">maroon</option>
                        <option value="navy">navy</option>
                        <option value="olive">olive</option>
                        <option value="purple">purple</option>
                        <option value="red">red</option>
                        <option value="silver">silver</option>
                        <option value="teal">teal</option>
                        <option value="white">white</option>
                        <option value="yellow">yellow</option>
                </select>
                <button onclick="setProperty(); return false;">設置背景顏色</button>
                <br />
                <br />
        </div>

</body>
</html>

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved