简体   繁体   中英

How to append html div to a table in response to jQuery getJSON

I'm trying to do the following:

$.getJSON("script/course_session.php", data, function(data) {
$.each(data, function(i, item) {

  var $tr = $('<tr>').append(
        $('<td>').text(item.start_date + " - " + item.end_date),
        $('<td>').text(
          '<div class=&quot;col-md-3 btn-buy animated fadeInRight&quot;><a href=&quot;#&quot; class=&quot;btn-u btn-u-sm enroll-button&quot;><i class=&quot;fa fa-university&quot;></i> Register</a></div>')
      ).appendTo("#session-table");
});
});

The intent being to achieve something like the following (I'm using Bootstrap):

在此处输入图片说明

However, this is how it gets printed: 在此处输入图片说明

What is the correct way to render html?

You have to use .html(...) instead of .text(). And " instead of quot;

.html()

A string of HTML to set as the content of each matched element.

.text()

The text to set as the content of each matched element. When Number or Boolean is supplied, it will be converted to a String representation.

The difference is the .html(str) will convert str the to a DOM if the str have a tag content and the .text() must convert str to a text even if str contains XML or html.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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