开发者

jQuery's setData/getData events should be capable of overriding $.data()'s default behavior... but how?

开发者 https://www.devze.com 2023-01-23 20:09 出处:网络
Check out the official blog post on the 1.4.3 release: http://blog.jquery.com/2010/10/16/jquery-143-released/

Check out the official blog post on the 1.4.3 release: http://blog.jquery.com/2010/10/16/jquery-143-released/

I am thoroughly confused by this sentence (under "Events"):

jQuery has already had setData and getData events (which are broadcast whenever data is set or gotten through the .data() method) – overriding these methods makes it possible to override the default behavior for those features (namely you can return a different value or prevent a value from being set).

  1. "(...) had setData and getData events (...) - overriding these methods" -- which methods?!?

  2. "makes it possible to override the default behavior" -- how?!? I have looked into the source and I cannot think of any way to either "return a different value" or "prevent a value from being set."

Anyone out 开发者_如何学Cthere who knows more than me?


You can subscribe to these events using the setData and getData events:

$(document.body).bind('setData', function(event, key, value) {
    console.log(key + ': ' + value);
});

This will log 'example: hello, world' when the following is called:

$('a:first').data('example', 'hello, world');

Unless I am completely misunderstanding how the code works, there is no way to change the default action, including e.preventDefault();. See the relevant lines in data.js, the source code for $.fn.data.

0

精彩评论

暂无评论...
验证码 换一张
取 消