简体   繁体   English

如何将html div附加到表以响应jQuery getJSON

[英]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): 目的是实现类似以下的功能(我正在使用Bootstrap):

在此处输入图片说明

However, this is how it gets printed: 但是,这是如何打印的: 在此处输入图片说明

What is the correct way to render html? 呈现html的正确方法是什么?

You have to use .html(...) instead of .text(). 您必须使用.html(...)而不是.text()。 And " instead of quot; 和“代替”

.html() .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. 区别在于,如果str具有标签内容,则.html(str)会将str转换为DOM,即使str包含XML或html, .text()必须将str转换为文本。

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

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