weixin_33725272 2013-04-04 10:18 采纳率: 0%
浏览 60

追加到appendTo

I am receiving a JSON object, inserting this info into a template and then appending this to a DIV. One of the elements that I'm inserting has the id set to device_id.

Once my template html has been inserted I want to select the DIV which has the ID=device_id and insert some further information into it.

I'm assuming the reason why this doesn't currently work is because the DOM hasn't yet updated by the time i'm attempting to select the div with the device_id, but I don't know how to deal with this.

var obj = $.parseJSON(msg);
$("#myTemplate").tmpl(obj).appendTo("#message");

jQuery.each(obj, function () {
    $('#' + this.device_id).append('<p>Hello</p>');
});

The above code is inside a click event. And the DOM will look like this:

<div id="message">
   <div class="item">
     <div id="#156846"></div>
     <div id="name"></div>
   </div>
</div>

The JSON being received (msg) looks like this:

[{"name":"Person A","device_type":"one", "device_id":"156846"}]

How do I insert the 'Hello' into the div where id=this.device_id?

  • 写回答

1条回答 默认 最新

  • George_Fal 2013-04-04 14:07
    关注

    Turns out that appendTo() was not the problem. If you can see above, has a typo. There should be no # as part of the id.

    Also, I needed to use $('#1234').find('#item').appendTo(); in order to get a child element that is further down in the hierarchy.

    评论

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误