![](/img/trans.png)
[英]Displaying json data from server in basic html table using for loop, javascript only, problem with for loop?
[英]Displaying actual html or link from encoded json data using javascript
我从ajax调用返回以下内容
{"dataList":[{"date":"August 27, 2013","text":"<a href=\"http:\/\/www.example.com\/test.aif\"
title=\"Click here to listen\" target=\"\">Click here to listen<\/a> to the test from
2013!"},],"record_count":3}
我正在通过循环将数据添加到屏幕上...
$.each(data.dataList, function(index, element) {
$('#news_data').append($('<h1>', {
text: element.date
}));
$('#news_data').append($('<h2>', {
text: element.text
}));
});
结果是页面显示所有html,而不显示实际URL。 所以我的问题是:如何获得它以实际显示链接而不是HTML? 我应该以其他格式返回数据吗?
与其使用text
属性,不如尝试使用html
属性,例如
$('#news_data').append($('<h1>', {
html: element.date
}));
$('#news_data').append($('<h2>', {
html: element.text
}));
然后,这会将HTML(因为您的数据是html)附加到保留标记的元素上。 这是对html()函数的引用,该函数的行为类似于html属性。 http://api.jquery.com/html/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.