按一次链接时在链接中隐藏图像

时间:2018-06-24 13:40:27

标签: javascript jquery html dom

我有以下脚本。我想要的是,当我按一下链接时,链接中的图像消失了。按下链接后我要消失的图像是enter.png。

<script>
$(document).ready(function() {
 $(".open_close_doors").click(function(){
   $("#leftdoor_inner").animate({"left": "-=395px"}, "slow");
   $("#rightdoor_inner").animate({"left": "+=395px"}, "slow");

   setTimeout("window.location.href='wall.php';",200);
 });
});
</script>

<a class="open_close_doors" href="#"><img src='img/enter.png' onmouseover=this.src='img/enter_light.png' onmouseout=this.src='img/enter.png'></a>

1 个答案:

答案 0 :(得分:2)

要隐藏图像,可以将其src属性设置为空:

this.children[0].src = "";

但是,由于您的目标是防止用户多次单击按钮,因此最好使整个链接消失:

this.style.visibility = 'hidden';

这是一个演示(出于演示目的,我注释掉了重定向):

$(document).ready(function() {
  $(".open_close_doors").click(function() {
    $("#leftdoor_inner").animate({
      "left": "-=395px"
    }, "slow");
    $("#rightdoor_inner").animate({
      "left": "+=395px"
    }, "slow");
    this.style.visibility = 'hidden';
    //setTimeout("window.location.href='wall.php';", 200);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class="open_close_doors" href="#"><img src='http://placehold.it/50x50' onmouseover="this.src='http://placehold.it/100x100'" onmouseout="this.src='http://placehold.it/50x50'"></a>