te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Does setting jQuery.data() trigger an event? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Does setting jQuery.data() trigger an event? - Stack Overflow

programmeradmin4浏览0评论

I'm wondering if calls to $(".domElement").data("key", "newValue") will trigger an event that I can handle? I've tried binding change but this doesn't get triggered when data is set.

I think this question may be asking something similar, but binding changeData didn't work either - jQuery data() and 'changeData' event.

I'm wondering if calls to $(".domElement").data("key", "newValue") will trigger an event that I can handle? I've tried binding change but this doesn't get triggered when data is set.

I think this question may be asking something similar, but binding changeData didn't work either - jQuery data() and 'changeData' event.

Share Improve this question edited Sep 24, 2022 at 8:20 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Jul 8, 2013 at 13:45 MattMatt 11.3k29 gold badges82 silver badges112 bronze badges 4
  • 4 I don't think it triggers an event, if you want then you can trigger a custom event, ex $(".domElement").data("key", "newValue").trigger('changeData') – Arun P Johny Commented Jul 8, 2013 at 13:47
  • Ok thanks, so then how can I access the original element that triggered the event? Usually I'd just use $(this) in the handler but it's showing up as undefined. – Matt Commented Jul 8, 2013 at 13:57
  • @Matt Look at the docs for trigger. You won't be able to set this, but you can pass extra parameters that you can get in the handler – Ian Commented Jul 8, 2013 at 14:00
  • Actually forget that - $(this) works fine when I bind my handler to the specific selector rather than just $(document). – Matt Commented Jul 8, 2013 at 14:03
Add a ment  | 

2 Answers 2

Reset to default 12

Actually you have only tried to attach the custom event but you will also have to trigger it something like:

$('button').click(function (e) {
    $('#someID').data("key", "newValue").trigger('changeData'); 
});

$('#someID').on('changeData', function (e) {    
    alert('My Custom Event - Change Data Called! for ' + this.id);
});

FIDDLE DEMO

It was automatic, up until version 1.8.3 (determined by searching the source for 'changeData').

However, it's written such that 'changeData' is triggered if you do:

$element.data('key', 'newValue');

but not if you pass an object, like:

$element.data({
    'key': 'newValue'
});

Edited source excerpts to illustrate this:

jQuery.fn.extend({
    data: function( key, value ) {

        // Gets all values
        if ( key === undefined ) {
                // expurgated
            }
        // Sets multiple values
        if ( typeof key === "object" ) {
            return this.each(function() {
                jQuery.data( this, key );
            });
        }

        return jQuery.access( this, function( value ) {

            if ( value === undefined ) {
                // expurgated
            }

            parts[1] = value;
            this.each(function() {
                var self = jQuery( this );

                self.triggerHandler( "setData" + part, parts );
                jQuery.data( this, key, value );
                self.triggerHandler( "changeData" + part, parts );
            });
        },
    }
});

I'm not pletely sure what jQuery.access does, but it looks to me (and testing confirmed) that the event only gets fired if you have passed the second 'newValue' argument.

发布评论

评论列表(0)

  1. 暂无评论