Tuesday, 15 April 2014

javascript - What is the best way to deactivate a click event from an -



javascript - What is the best way to deactivate a click event from an -

on webpage have container turns else when clicked, , intent remain way after clicked:

<div id="macheps-holder"> <p>click here see browser's machine epsilon!</p> <script type="text/javascript"> $(document).ready() { var temp1 = 1.0, temp2, macheps; { macheps = temp1 temp1 /= 2 temp2 = 1.0 + temp1 } while (temp2 > 1.0) var mh = $('#macheps-holder'); mh.click(function() { mh.html("<p>your browsers machine epsilon <code>" + macheps + "</code></p>"); mh.click(function() { return; }) }); } </script> </div>

also can see, did create body of click function alter click function return;, i.e. nothing. i'm wondering if there's more elegant , proper way of accomplishing behavior.

thank time.

at first jquery's ready function not correct. should pass callback it, this:

$(document).ready(function() { ... })

the second. can create separate function callback click event , add together or remove event listener list. can write following:

function clickhandler(event) { ... } // add together mh.on('click', clickhandler); // remove mh.off('click', clickhandler);

the final code this:

class="snippet-code-js lang-js prettyprint-override">$(document).ready(function() { var temp1 = 1.0, temp2, macheps; { macheps = temp1 temp1 /= 2 temp2 = 1.0 + temp1 } while (temp2 > 1.0) var mh = $('#macheps-holder'); function clickhandler() { mh.html("<p>your browsers machine epsilon <code>" + macheps + "</code></p>"); mh.off('click', clickhandler); // remove click event handler } mh.on('click', clickhandler); // add together click event handler })

you can read more on , off functions of jquery.

javascript jquery

No comments:

Post a Comment