简体   繁体   English

jQuery在将数据添加到DOM之前将数据添加到html

[英]Jquery add data to html before adding to DOM

I am building html on the fly need to add data before I add it to DOM. 我正在动态构建html,需要先添加数据,然后再将其添加到DOM。 Since I am looping thru' lot of information, I would like to add the relevant data info along with the dom I am building instead of adding the html and then looping thru again to add the data. 由于我要遍历大量信息,因此我想添加相关数据信息以及正在构建的dom,而不是添加html,然后再次遍历以添加数据。

result.forEach(function(record) {
html += '<div id ="' record.ID + '">test content </div> ';

//add data to above
 });

I can do another loop here after adding it to DOM 将其添加到DOM之后,我可以在此处执行另一个循环

$(body).append(html);
 testresult.forEach(function(record) {
   $("#" +record.ID).data(record);
 });

You have quotes problem in the following line : 您在以下行中存在引号问题:

html += '<div id =record.testID' + '>test content </div> ';
________^______________________^___^_____________________^

You should fix that using double quotes because as it's now the string will be considered as '<div id =record.testID' . 您应该使用双引号将其修复,因为到目前为止,该字符串将被视为'<div id =record.testID'

html += '<div id="'+record.testID+'">test content </div>';

Or you could use separated definition : 或者您可以使用单独的定义:

$.each(result, function(index,record) {
  var div = $('<div>test content</div>'); 

  div.attr('id', record.testID);
  div.data('test', record.testDATA);

  $('body').append(div);
})

Hope this helps. 希望这可以帮助。

 var result = [{testID: 1,testDATA: 'data 1'},{testID: 2,testDATA: 'data 2'},{testID: 3,testDATA: 'data 3'}] var html=''; $.each(result, function(index,record) { var div = $('<div>test content</div>'); div.attr('id', record.testID); div.data('test', record.testDATA); console.log(div.data('test')); $('body').append(div); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

Instead of concatenating strings to piece together your HTML, you may way to try something like this: 与其串联字符串以将HTML拼凑起来,不如尝试以下方法:

result.forEach(function(record) {
    $('.selector').append(function () {
        var $div = $('<div></div>');
        $div.attr('id', record.testID).text('some text');

        return $div;
    });
 });

This creates a new div jquery object for each item in result. 这将为结果中的每个项目创建一个新的div jquery对象。 You can use the record object to add attributes, data, text, etc to you object. 您可以使用record对象向对象添加属性,数据,文本等。 It will be added the DOM when the callback passed into .append returns your new jquery DOM object. 当传递给.append的回调返回新的jquery DOM对象时,它将添加DOM。

Start trying to use jQuery to create your html elements so you can take fully advantage of jQuery and its plugins. 开始尝试使用jQuery创建html元素,以便您可以充分利用jQuery及其插件。

Ex: 例如:

var div = $("<div></div>")         // create the element
    .text("test content")          // change the inner text
    .attr("id", record.testID);    // set the element id

div.appendTo("body");

You can check out [ http://www.w3schools.com/jquery/] as a great source for learning jQuery. 您可以查看[ http://www.w3schools.com/jquery/],作为学习jQuery的绝佳资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM