动态更改img标签';带Javascript的onemouseover属性将其改为null

动态更改img标签';带Javascript的onemouseover属性将其改为null,javascript,null,image,onmouseover,Javascript,Null,Image,Onmouseover,我想创建一个具有四种不同状态的img: 状态1结束 陈述1 状态2结束 陈述2 每个状态都是不同的图像,用户可以通过单击图像在状态1和状态2之间切换。 为此,我在单击图像时更改src,并更改img元素的onmouseover和onmouseout属性。但是,当属性被更改时,它们将变为null,并且不执行任何操作。如何动态更改这些属性 以下是我正在使用的代码: <!DOCTYPE html> <html> <head> <script> functi

我想创建一个具有四种不同状态的img: 状态1结束 陈述1 状态2结束 陈述2

每个状态都是不同的图像,用户可以通过单击图像在状态1和状态2之间切换。 为此,我在单击图像时更改src,并更改img元素的onmouseover和onmouseout属性。但是,当属性被更改时,它们将变为null,并且不执行任何操作。如何动态更改这些属性

以下是我正在使用的代码:

<!DOCTYPE html>
<html>
<head>
<script>
function change()
{ 
document.getElementById('image').src="http://youtube.thegoblin.net/layoutFix/hideplaylist.png";
document.getElementById('image').onmouseover="this.src='http://youtube.thegoblin.net/layoutFix/hideplaylistDark.png'";
document.getElementById('image').onmouseout="this.src='http://youtube.thegoblin.net/layoutFix/hideplaylist.png'";
}
</script>
</head>

<body>
<img id="image" src="http://youtube.thegoblin.net/layoutFix/showplaylist.png" onmouseover="this.src='http://youtube.thegoblin.net/layoutFix/showplaylistDark.png'" onmouseout="this.src='http://youtube.thegoblin.net/layoutFix/showplaylist.png'" onClick="change()">

</body>
</html>

函数更改()
{ 
document.getElementById('image').src=”http://youtube.thegoblin.net/layoutFix/hideplaylist.png";
document.getElementById('image').onmouseover=“this.src=”http://youtube.thegoblin.net/layoutFix/hideplaylistDark.png'";
document.getElementById('image').onmouseout=“this.src=”http://youtube.thegoblin.net/layoutFix/hideplaylist.png'";
}

如果我很理解您的问题,您需要一个变量进行递增,并检查它是奇数还是偶数,然后根据您的需要进行不同的处理。

您需要使用两个变量来保存切换状态:

var img = document.getElementById("image");
var toggleover  = false;
var toogleClick = false;

img.addEventListener('click',function(){
    toogleClick = !toogleClick;
    changeImg();
},false);

img.addEventListener('mouseover',function(){
    toggleover = true;
    changeImg();
},false);
img.addEventListener('mouseout',function(){
    toggleover = false;
    changeImg();
},false);

(function changeImg(){
if(toogleClick)  
    img.src = toggleover ? "url-1-2.jpg" : "url-1-1.jpg";
else
    img.src = toggleover ? "url-2-2.jpg" : "url-2-1.jpg";
})();

我一点也不明白你的问题不够清楚,你应该详细解释每个州你想使用的图像。由于您总共有6个图像,您需要为每个状态编写要与之关联的图像。在javascript中,您没有分配
onmouseover
属性,而是试图为mouseover设置事件处理程序(它必须始终是一个函数)。无论如何,这对我没有帮助,更改src是可以的,当我更改onmouseover和onmouseout属性时,它不起作用。