当用户将鼠标悬停在链接上时,我正在尝试换掉div上的背景图片。

我究竟做错了什么?这不起作用。

<style>
#divToSwap {
    background-image: url(black.jpg);
    height: 150px;
    width: 150px;
}
</style>
</head>

<body>
<div id="divToSwap"></div>
<div>
<ul>
  <li><a href="#"
        onmouseover="document.getElementById('divToSwap').backgroundImage='url(red.jpg)';"
        onmouseout="document.getElementById('divToSwap').backgroundImage='url(black.jpg)';">
        Red</a></li>
  <li><a href="#"
        onmouseover="document.getElementById('divToSwap').backgroundImage='url(blue.jpg)';"
        onmouseout="document.getElementById('divToSwap').backgroundImage='url(black.jpg)';">
        Blue</a></li>
  <li><a href="#"
        onmouseover="document.getElementById('divToSwap').backgroundImage='url(green.jpg)';"
        onmouseout="document.getElementById('divToSwap').backgroundImage='url(black.jpg)';">
        Green</a></li>
</ul>
</div>

最佳答案

尝试,例如

document.getElementById('divToSwap').style.backgroundImage='url(black.jpg)';

10-06 07:40