我正在使用Javascript在URL中查找“#mode2”,如果是这样,它将产生与画布不同的彩色图像。脚本的这部分工作正常。问题出在产生切换超链接的脚本中。它会第一次正确切换(作为脚本的不同部分,这是有意义的),然后根据它开始的位置切换一次或两次。Javascript切换不起作用
I.E.蓝色是默认的,所以如果页面加载蓝色,它将切换橙色,然后回到蓝色,然后不起作用,就像在橙色它将切换到蓝色,然后将无法正常工作。
我知道它在我的代码中可能有些愚蠢的错误,但我找不到它,所以如果有人能帮助我,我会很感激。
<script type="text/javascript">
function toggle(blue){
if (blue == 0) {
blue = 1;
}
else if (blue == 1) {
blue = 0;
}
if (blue == 0){
var container = document.getElementById("navi");
var content = '<a href="contact.html#mode1" title="toggle" onclick="toggle(blue);" >toggle colors</a> - <a href="index.html#mode2" title="home">back</a>';
container.innerHTML= content;
}
else{
var container = document.getElementById("navi");
var content = '<a href="contact.html#mode2" title="toggle" onclick="toggle(blue);" >toggle colors</a> - <a href="index.html" title="home">back</a>';
container.innerHTML= content;
}
}
var pathArray = document.URL;
if (pathArray.indexOf("mode2") != -1){
var blue = 0;
var container = document.getElementById("navi");
var content = '<a href="contact.html#mode1" title="toggle" onclick="toggle(blue);" >toggle colors</a> - <a href="index.html#mode2" title="home">back</a>';
container.innerHTML= content;
}
else{
var blue = 1;
var container = document.getElementById("navi");
var content = '<a href="contact.html#mode2" title="toggle" onclick="toggle(blue);">toggle colors</a> - <a href="index.html" title="home">back</a>';
container.innerHTML= content;
}
</script>
[jsfiddle](http://jsfiddle.net)会有所帮助。 – bjb568
向我们显示您的HTML – gfrobenius