jQuery将.data加载到子元素

时间:2014-11-12 02:22:54

标签: javascript jquery parent-child

我的index.html在这里:

<tr class="activity_row" t-att-data-activity_id="activity.id">
   <td>
       <div>
           <t t-if="duration != undefine">
              <span class="pt_duration_line"><t t-esc="duration.split(':')[0] + 'h ' + (duration.split(':')[1] and duration.split(':')[1] + 'min' or '')"></t></span> <!-- call format_hour method of this screen -->
           </t>
       </div>
   </td>
</tr>

对于第一个 tr 标记,onclick操作为:

this.$el.find(".activity_row").on("click", this.on_row_click);

on_row_click: function(event) {
    var activity_id = $(event.currentTarget).data("activity_id");
    if(activity_id) {
        var activity = this.project_timesheet_db.get_activity_by_id(activity_id);
        this.project_timesheet_widget.screen_selector.set_current_screen("add_activity", activity);
    }
},

我在这里成功获得 activity_id 。但是在这一行内部有一个内部 div ,其中包含顶部提到的包含 pt_duration_line 类的范围。为此,onclick行动是:

this.$el.find(".pt_duration_line").on("click", this.on_url_click);

    on_url_click: function(event) {
        var act_id = $(event.currentTarget).data("act_id");
        if(act_id) {
            var activity = this.project_timesheet_db.get_activity_by_id(act_id);

对于这个内部 div 我没有获得&#39; act_id&#39;我正在寻找的活动&#39; activity_id&#39;对于父元素

简而言之:如果我想在 div 中获得 activity_id 该行,该怎么办? 提前致谢

1 个答案:

答案 0 :(得分:1)

试试这个:

<tr class="activity_row" t-att-data-activity_id="activity.id">
   <td>
       <div>
           <t t-if="duration != undefine">
              <span class="pt_duration_line" t-att-data-act-id="activity.id"><t t-esc="duration.split(':')[0] + 'h ' + (duration.split(':')[1] and duration.split(':')[1] + 'min' or '')"></t></span> <!-- call format_hour method of this screen -->
           </t>
       </div>
   </td>
</tr>

另外,正如其他用户提到的,e和事件不一样,改为:

this.$el.find(".pt_duration_line").on("click", this.on_url_click);

    on_url_click: function(event) {
        var act_id = $(event.currentTarget).data("act_id");
        if(act_id) {
            var activity = this.project_timesheet_db.get_activity_by_id(act_id);