我有两个按钮显示和隐藏,我有一个图像所以我想知道当我点击隐藏按钮时,img将消失,当我点击显示按钮时它将再次出现。我不想用CSS或JQUERY只做Javascript。如果有人可以帮助感谢很多。
答案 0 :(得分:1)
window.onload = function(){
var myImg = document.getElementById('myImg');
document.getElementById('hideBtn').onclick = function(){
myImg.style.display = 'none';
};
document.getElementById('showBtn').onclick = function(){
myImg.style.display = '';
};
document.getElementById('toggleBtn').onclick = function(){
var display = getComputedStyle(myImg).display=='none'?'':'none';
myImg.style.display = display;
};
}

<button id="hideBtn">Hide</button>
<button id="showBtn">Show</button>
<button id="toggleBtn">Toggle</button>
<br/>
<img id="myImg" src="http://www.eastcottvets.co.uk/uploads/Animals/gingerkitten.jpg" alt="" />
&#13;
答案 1 :(得分:1)
function show(){
document.getElementById('image').style.display = "inline";
}
function hide(){
document.getElementById('image').style.display = "none";
}
<img src="http://colorvisiontesting.com/images/plate%20with%205.jpg" width="80px" height="80px" id="image"/><br />
<button onClick="hide();">Hide</button>
<button onClick="show();">Show</button>