如何调试JavaScript代码中的单击事件处理?

时间:2016-05-11 20:22:54

标签: javascript html

    <img id="Slide" src="SlideOne.png"></img>

<img id="SImg" src="Slide Image One.png" onmouseover="this.src='Slide Image Hover One.png';" onmouseout="this.src='Slide Image One.png';" onclick="SlideManager()"></img>
<img id="SImg" src="Slide Image Two.png" onmouseover="this.src='Slide Image Hover Two.png';" onmouseout="this.src='Slide Image Two.png';" onclick="SlideManager()"></img>
<img id="SImg" src="Slide Image Three.png" onmouseover="this.src='Slide Image Hover Three.png';" onmouseout="this.src='Slide Image Three.png';" onclick="SlideManager()"></img> 

</center>

<script>
function SlideManager() {
    if(document.getElementById('SImg').src.match("Slide Image One.png"))
    {
        document.getElementById('Slide').src = "SlideOne";
    }
    if(document.getElementById('SImg').src.match("Slide Image Two.png"))
    {
        document.getElementById('Slide').src = "SlideTwo";
    }
    if(document.getElementById('SImg').src.match("Slide Image Two.png"))
    {
        document.getElementById('Slide').src = "SlideTwo";
    }
}
</script>

我第一次使用JavaScript。当点击特定按钮/图像时,它应该更改图像。

但它不起作用。

2 个答案:

答案 0 :(得分:0)

你可能想这样做:

<img id="Slide" src="Slide Image One.png" />

<!-- id has to be unique, cannot use same id for multiple elements -->
<img id="Img1" src="Slide Image One.png" onmouseover="changeImage('Slide Image Hover One.png');" onmouseout="changeImage(currentSlide)" onclick="setCurrentSlide('Slide Image Hover One.png')" />
<!-- also <img /> is single-tag element -->

<img id="Img2" src="Slide Image Two.png" onmouseover="changeImage('Slide Image Hover Two.png');" onmouseout="changeImage(currentSlide)" onclick="setCurrentSlide('Slide Image Hover Two.png')" />
<img id="Img3" src="Slide Image Three.png" onmouseover="changeImage('Slide Image Hover Three.png');" onmouseout="changeImage(currentSlide)" onclick="setCurrentSlide('Slide Image Hover Three.png')" /> 

</center>

<script>
// use variable to remember current active slide (to be used to return to by mouseout)
var currentSlide = "Slide Image One.png";

// just temporarly change
function changeImage(name) {
    document.getElementById('Slide').src = name;
}

// save new image to currentSlide and change image
function setCurrentSlide(name) {
    currentSlide = name;
    changeImage(currentSlide);
}
</script>

答案 1 :(得分:0)

我想在点击“Slide Image Two”按钮时将“SlideOne”图像更改为“SlideTwo”。