本文介绍了jQuery .data() 不更新 HTML5 数据属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 jQuery 向 div 添加数据绑定"属性,如下所示:

var messageViewModel = {数据:ko.observable({消息:响应消息,sendDateFmted: response.sendDateFmted,messageId: response.messageId})};$("

",{类:messageToAndFromOtherMember"}).data("bind", "template: { name: 'message-template', data: data }").appendTo("#messagesToAndFromOtherMember");ko.applyBindings(messageViewModel);

"data-bind" 是 KnockoutJs 所必需的.但是我得到的只是这个空的 div:

<div class="messageToAndFromOtherMember"></div>

注意没有data-bind这样的属性,因此div保持为空...

解决方案

jQuery 的 .data() 将值存储在内存中并使用 data-* 属性进行初始化.您可能希望通过在元素创建时设置它来坚持下去.

$("

", {类:messageToAndFromOtherMember",数据绑定":模板:{名称:'消息模板',数据:数据}"}).appendTo("#messageToAndFromOtherMember");

I am trying to add a "data-bind" attribute to a div using jQuery as follows:

var messageViewModel = {
    data: ko.observable({
        message: response.message,
        sendDateFmted: response.sendDateFmted,
        messageId: response.messageId
    })
     };

$("<div>",{
    class:"messageToAndFromOtherMember"
}).data("bind", "template: { name: 'message-template', data: data }").appendTo("#messagesToAndFromOtherMember");

ko.applyBindings(messageViewModel);

"data-bind" is required by KnockoutJs. However all I get is this empty div:

<div class="messageToAndFromOtherMember"></div>

Notice there is no such attribute as data-bind and therefore the div remains empty...

解决方案

jQuery's .data() stores the values in-memory and uses data-* attributes for initialization. You may want to stick by setting it at element creation.

$("<div/>", {
  class: "messageToAndFromOtherMember",
  "data-bind": "template: { name: 'message-template', data: data }"
}).appendTo("#messageToAndFromOtherMember");

这篇关于jQuery .data() 不更新 HTML5 数据属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 04:19