简体   繁体   中英

Activate/Deactivate onclick event via Javascript?

I try to do like a questionnary, when you click on 1 answer, that check if it's the good answer and then desactivate the onclick event, after clicking on an other button to change the question and add new answer i want to reactivate the click but i dont know how to do this in javascript :/

Here the code for better explanation x):

<ol id="answerList" type="A">
      <img class="imgLine" src="" id="answer-a-img" /><li onclick="clickLine(this);" id="answer-a" ></li>
      <img class="imgLine" src="" id="answer-b-img" /><li onclick="clickLine(this);" id="answer-b" ></li>
      <img class="imgLine" src="" id="answer-c-img" /><li onclick="clickLine(this);" id="answer-c" ></li>
      <img class="imgLine" src="" id="answer-d-img" /><li onclick="clickLine(this);" id="answer-d" ></li>
</ol>

Here is the answer and the onclick event is working and that call clickLine function

I desactivated the onclick event by doing this: (i know it's not clean but i didnt saw an other way :/)

document.getElementById('answer-a').onclick = "";
document.getElementById('answer-b').onclick = "";
document.getElementById('answer-c').onclick = "";
document.getElementById('answer-d').onclick = "";

but now, when i got new datas, i dont success to reactivate onclickevent, i tried this:

document.getElementById('answer-a').onclick = "clickLine(this);";
document.getElementById('answer-b').onclick = "clickLine(this);";
document.getElementById('answer-c').onclick = "clickLine(this);";
document.getElementById('answer-d').onclick = "clickLine(this);";

but didnt worked :/

thanks !

ps: I can use Jquery if there is a solution in it ^^

Something like:

    $(".imgLine").click(function(event)
    {
         $('.imgLine').unbind();
         event.preventDefault();
    });

    $("a#activate").click(function(event)
    {
        $('.imgLine').bind();        
        event.preventDefault();
    });

Where a#activate denotes the next question button.

document.getElementById('answer-a').onclick = "javascript:clickLine(this);";
document.getElementById('answer-b').onclick = "javascript:clickLine(this);";
document.getElementById('answer-c').onclick = "javascript:clickLine(this);";
document.getElementById('answer-d').onclick = "javascript:clickLine(this);";

Try to Execute like this. First check do you have javascript:clickLine() function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM