goo*_*ing -3 javascript php jquery json
我在PHP中创建了一个包含大量HTML的表.创建之后,我将其作为JSON字符串回显如下:
echo json_encode($array['table']);
Run Code Online (Sandbox Code Playgroud)
然后我使用jQuery.parseJSON(returnString);并尝试将其附加到div.这只有在我返回一个字符串时才有效.当我发送HTML时它不起作用(在这种情况下根本没有返回任何内容).
谁能告诉我出了什么问题?\ / " '在将符号发送到jQuery之前,是否需要使用某些函数来解析符号?
编辑:
我$arr['table']看起来像这样:
$arr['table'] = "<tr class=\"{$id}\">lalalal</tr>"
Run Code Online (Sandbox Code Playgroud)
我的JSON看起来像这样:
\n\t\t\t\t\t</div>\n\t\t\t\t\tfcds\n\t\t\t\t\t</center>\
Run Code Online (Sandbox Code Playgroud)
您不应该通过JSON发送HTML数据,而只是"数据",然后使用javascript创建新的HTML元素并使用这些新元素修改dom.
一个jQuery示例:
var json_doc = $.getJSON('http://yourservice.com/yo/');
var tr = $(document.createElement('tr'));
tr.attr('id', json_doc.responseText.id);
tr.html(json_doc.responseText.html);
$('table').append(tr);
Run Code Online (Sandbox Code Playgroud)
您的回复http://yourservice.com/yo/将是:
{id: 1, html: "lalalal"}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3474 次 |
| 最近记录: |