I am a newbie in javascript. I have a script like this:
<html>
<head>
<script type="text/javascript">
var x= '<img id="im" src="thumbnail.gif">';
switch (x)
{
case x=='document.getElementById("im")':
document.getElementById('im').outerHTML='<img id="im" src="fullsize.gif">';
break;
case x!='document.getElementById("im")':
document.getElementById('im').outerHTML='<img id="im" src="thumbnail.gif">';
break;
default: x;
}
</script>
</head>
<body>
<span onclick="switch()">
<img id="im" src="thumbnail.gif">
</span>
</body>
</html>
I need to keep it simple, but also make it work using the switch function. I didn't used them im my script. I am NOT looking for开发者_运维问答 a slide show, also.
For one, you don't want quotes around 'document.getElementById("im")'
since that means you're looking for that string. Two, case statements don't work like that, the syntax is case value
, not case expression
(an expression being your ==
). Three, you don't have an actual function call. You're calling switch
which doesn't do anything...
Try something like:
function swap(){
switch(document.getElementById('im').outerHTML){
case x:
...
break;
default:
...
break;
}
}
...
<span onclick="swap();">
which should fix your existing code. Or the "proper" way of doing this is more like:
function swap(){
var node = document.getElementById('im');
node.src = node.src.indexOf('fullsize')>=0 ? 'thumbnail.gif' : 'fullsize.gif';
}
...
<span onclick="swap();">
Oh, and I fixed your original question.
精彩评论