那是因为您只能拥有一个具有给定 id 的元素。
你应该使用类。
见http://jsfiddle.net/mwu4M/
HTML
把那些丑陋的
和<br />
. 并使用class="imageshow"
而不是id="imageshow"
.
<button class="imageshow">Button1</button>
<button class="imageshow">Button2</button>
<button class="imageshow">Button3</button>
<img id="myimg" src="Images/Image1.jpg" onmouseover="showIt(this.src)" onmouseout="hideIt()" alt="[IMG]">
<button class="imageshow">Button4</button>
<button class="imageshow">Button5</button>
<button class="imageshow">Button6</button>
CSS:
.imageshow{
visibility:hidden;
margin-right:15px;
}
.imageshow.show{
visibility:visible;
}
#myimg{display:block;}
JavaScript:
function getElementsByClassName(c,el){
if(typeof el=='string'){el=document.getElementById(el);}
if(!el){el=document;}
if(el.getElementsByClassName){return el.getElementsByClassName(c);}
var arr=[],
allEls=el.getElementsByTagName('*');
for(var i=0;i<allEls.length;i++){
if(allEls[i].className.split(' ').indexOf(c)>-1){arr.push(allEls[i])}
}
return arr;
}
function addClass(el,c){
var arr=el.className.split(' ');
if(arr.indexOf(c)>-1){return;}
arr.push(c);
el.className=arr.join(' ');
}
function delClass(el,c){
var arr=el.className.split(' ');
if(arr.indexOf(c)===-1){return;}
arr.splice(arr.indexOf(c),1);
el.className=arr.join(' ');
}
function showIt(imgsrc){
this.src=imgsrc;
var els=getElementsByClassName('imageshow');
for(var i=0;i<els.length;i++){
addClass(els[i],'show');
}
}
function hideIt(){
var els=getElementsByClassName('imageshow');
for(var i=0;i<els.length;i++){
delClass(els[i],'show');
}
}
编辑:
甚至更好:http: //jsfiddle.net/mwu4M/1/
HTML:
<div id="wrapper">
<button class="imageshow">Button1</button>
<button class="imageshow">Button2</button>
<button class="imageshow">Button3</button>
<img id="myimg" src="Images/Image1.jpg" onmouseover="showIt(this.src)" onmouseout="hideIt()" alt="[IMG]">
<button class="imageshow">Button4</button>
<button class="imageshow">Button5</button>
<button class="imageshow">Button6</button>
</div>
CSS:
#wrapper>.imageshow{
visibility:hidden;
margin-right:15px;
}
#wrapper.show>.imageshow{
visibility:visible;
}
#myimg{display:block;}
JavaScript:
function showIt(imgsrc){
this.src=imgsrc;
document.getElementById('wrapper').className="show";
}
function hideIt(){
document.getElementById('wrapper').className="";
}
但我不明白你在做什么src
。
你的代码有
document.getElementById('imageshow').src=imgsrc;
但是imageshow
是按钮而不是图像!
然后我以为你想改变myimg
'src.
但这没有意义,因为我们将其更改src
为src
!!
<img id="myimg" src="Images/Image1.jpg" onmouseover="showIt(this.src)" onmouseout="hideIt()" alt="[IMG]">
function showIt(imgsrc){
this.src=imgsrc;/*It was "Images/Image1.jpg" and now it's "Images/Image1.jpg" too*/
document.getElementById('wrapper').className="show";
}
编辑2:
如果您想在悬停图像时显示按钮并在鼠标移动到另一个位置时隐藏它们,您可以onmouseleave
在父级上使用。但是并非所有浏览器都支持它,因此您需要实现它:
function containsOrIs(el1,el2){
if(el1===el2){return true;}
return Boolean(el1.compareDocumentPosition(el2)&16);
}
function addEvent(el,e,f,b){
if(typeof el==='string'){el=document.getElementById(el);}
if(e=='mouseenter'||e=='mouseleave'){
return addEvent(el,e=='mouseenter'?'mouseover':'mouseout',function(e){
if(!e){e=window.event;}
if((el===e.target || containsOrIs(el, e.target)) && !containsOrIs(el, e.relatedTarget||e[e=='mouseenter'?'fromElement' : 'toElement'])){
f();
}
});
}
if(el.addEventListener){
if(b==undefined){b=false}
return el.addEventListener(e,f,b);
}
if(el.attachEvent){
return el.attachEvent('on'+e,f);
}
}
function showIt(imgsrc){
this.src=imgsrc;
document.getElementById('wrapper').className="show";
}
function hideIt(){
document.getElementById('wrapper').className="";
}
window.onload=function(){
addEvent('wrapper','mouseleave',hideIt);
}
演示:http://jsfiddle.net/mwu4M/2/
编辑 3:
但也许您更喜欢使用:hover
,它更简单且仅 CSS:
#wrapper>.imageshow{
visibility:hidden;
}
#wrapper:hover>.imageshow{
visibility:visible;
}
但是当鼠标悬停时,按钮将显示#wrapper
,而不仅仅是图像。
演示:http://jsfiddle.net/mwu4M/3/