如何在同一元素中调用onclick两次

时间:2016-02-29 16:04:24

标签: javascript onclick

我试图这样做,当你点击这个td时,changeImage()函数就会到位。问题是,当你再次点击相同的td标签时,我不知道如何将其设置为默认值 - 将其恢复为orignal。

<table id="table">
    <tr>
        <td id="s1" class="kvadratek" onclick="changeImage();">?</td>
        <td id="s2" class="kvadratek slike">?</td>
        <td id="s3" class="kvadratek slike">?</td>
        <td id="s4" class="kvadratek slike">?</td>
    </tr>

 <script type="text/javascript">
    function changeImage(){
        document.getElementById("s1").style.backgroundImage = "url('css/image1.jpg')";
        document.getElementById("s1").style.backgroundSize = "contain";
        document.getElementById("s1").innerHTML = "";

    }

2 个答案:

答案 0 :(得分:2)

您只需要设置一个切换

Permission denied, please try again.
Permission denied, please try again.
Permission denied (publickey,password).
rsync: connection unexpectedly closed (0 bytes received so far) [sender] 
rsync error: unexplained error (code 255) at io.c(605) [sender=3.0.9]

答案 1 :(得分:0)

function changeImage(){
    changed = (bool) document.getElementById("s1").innerHTML;
    document.getElementById("s1").style.backgroundImage = changed ? '' : "url('css/image1.jpg')";
    document.getElementById("s1").style.backgroundSize = changed ? '' :"contain";
    document.getElementById("s1").innerHTML = changed ? '?' : '';
}