0

我正在尝试使用下拉菜单更改正文的背景图像(id =“shelf”):

<script type="text/javascript">
function changeTheme()
{document.getElementById("shelf").style.backgroundImage = "url("+theme+")";
  var e = document.getElementById("themes");
  var theme = e.options[e.selectedIndex].value;
}  
</script>
</head>
<body id="shelf">
<select id="themes" onChange="changeTheme()">
  <option value="images/bg/default.png">Default</option>
  <option value="images/bg/oriental.png">Oriental</option>
  <option value="images/bg/office.png">Office</option>
  <option value="images/bg/old.png">Old</option>
</select>
</body>    

但我不知道为什么它不起作用..代码中的问题在哪里?

4

2 回答 2

0

您看到的具体行为是什么?

尝试这个:

<html>
<head>
<script type="text/javascript">
function changeTheme()
{

  var e = document.getElementById("themes");
  var theme = e.options[e.selectedIndex].value;
  console.log(theme);
  document.getElementById("shelf").style.backgroundImage = "url("+theme+")";
}  
</script>
</head>
<body id="shelf">
<select id="themes" onChange="changeTheme()">
  <option value="images/bg/default.png">Default</option>
  <option value="images/bg/oriental.png">Oriental</option>
  <option value="images/bg/office.png">Office</option>
  <option value="images/bg/old.png">Old</option>
</select>
</body>    
</html>
于 2012-12-16T16:11:54.790 回答
0

它正在工作。

function changeTheme() {
    var e = document.getElementById("themes");
    var theme = e.options[e.selectedIndex].value;
    document.getElementById("shelf").style.backgroundImage = "url(" + theme + ")";

}
于 2012-12-16T16:13:13.220 回答