最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - possible clear jquery `one` method - Stack Overflow

programmeradmin2浏览0评论

Its possible to clear jquery one property? for example given html

    <div id="button">button</div>

    <div id="clearOneProperty">clear one property</div>

and js

$("#button").one("click", function () {
    alert("blah");
});

$("#clearOneProperty").on("click", function () {
    // clear "one" property, that is after this event,  I want "#button"  to work again
});

Demo /

So, if click #button div, alert happened only one times right?

And I want that, at click on #clearOneProperty, reset one property. Its possible?

P.S. I am not asking how to make this with other ways, I am interest exact: "possible clear jquery one method?". Thanks.

Its possible to clear jquery one property? for example given html

    <div id="button">button</div>

    <div id="clearOneProperty">clear one property</div>

and js

$("#button").one("click", function () {
    alert("blah");
});

$("#clearOneProperty").on("click", function () {
    // clear "one" property, that is after this event,  I want "#button"  to work again
});

Demo http://jsfiddle/RzzCu/2/

So, if click #button div, alert happened only one times right?

And I want that, at click on #clearOneProperty, reset one property. Its possible?

P.S. I am not asking how to make this with other ways, I am interest exact: "possible clear jquery one method?". Thanks.

Share Improve this question edited May 2, 2014 at 6:31 Dharmraj 5866 silver badges26 bronze badges asked Nov 30, 2012 at 9:58 Oto ShavadzeOto Shavadze 42.8k55 gold badges165 silver badges246 bronze badges 2
  • 2 When you say "clear one() method" do you mean, re-attach it so that #button can be clicked again, or do you mean remove the one() event so that it cannot be clicked? – Rory McCrossan Commented Nov 30, 2012 at 10:00
  • re-attach it so that #button can be clicked again – Oto Shavadze Commented Nov 30, 2012 at 10:01
Add a ment  | 

5 Answers 5

Reset to default 7

Try this:

function bindButton() {
    $("#button").unbind('click').one("click", function() {
        alert("blah");
    });
}

bindButton();

$("#clearOneProperty").on("click", function() {
    bindButton();
});

Updated fiddle

The one unbinds its self on first invocation as stated "The first form of this method is identical to .bind(), except that the handler is unbound after its first invocation", jQuery Documentation *So you need to bind it again*.

Live Demo

$("#button").one("click", onefunction);

function onefunction() {
    alert("blah");
}
$("#clearOneProperty").one("click", function() {       
    $("#button").one("click", onefunction);
});​

Just rebind it inside the function.

$("#button").one("click", function () {
    alert("blah");
});

$("#clearOneProperty").one("click", function () {
    $('#button').one("click", function () {
    alert("blah");
});
});

here a fiddle

Use unbind() method

$("#button").unbind();

Try

$('#button').unbind('click');
发布评论

评论列表(0)

  1. 暂无评论