当用户在Javascript中单击图像时,如何更改div的div图像

时间:2012-02-28 09:18:26

标签: javascript

我在css中有一个div图像和另一个小图像。如果我点击小图片,我希望我的div图像改变。

如何为此编写脚本?

2 个答案:

答案 0 :(得分:0)

var div=document.getElementById('yourDiv');
var height = div.style.height;
var width = div.style.width;
var enlargedHeight = height + *size to increase goes here*;
var enlargedWidth = width + *size to increase goes here*;

function grow() {
if (div) {
div.style.backgroundImage = 'url(newimage.gif)';
height=enlargedHeight; 
width=enlargedWidth;
}
}

和你的div

<div id="yourDiv" onclick="grow();">
/*content*/
</div>

答案 1 :(得分:0)

只需编写一个onclick处理程序,如:

document.getElementById ('theSmallImageId').onclick = function{
document.getElementById ('TheDivId').style.backgroundImage = urlToTheNewImage; 
};

我希望这有用。