gpt4 book ai didi

jquery - jQuery 数据表中不允许使用小于 (<) 符号

转载 作者:行者123 更新时间:2023-12-01 05:44:02 25 4
gpt4 key购买 nike

有一个小要求,我想用 JSON 数据显示数据表,在这里我有一个小挑战,如果 JSON 数据有特殊字符,如 lessthan symbol(<) ,数据不显示在数据网格中。不知道为什么只有 lessthan symbol(<) 才会出现问题我尝试使用以下代码,如果有任何遗漏,请纠正我,例如:姓氏是 Jhons<asdf但它只显示 Jhons请帮我解决这个问题。

这是我的示例代码

$.ajax({
url: '/echo/json/',
type: "post",
dataType: "json",
data: {
json: JSON.stringify([
{
id: 1,
firstName: "Peter&heins",
lastName: "Jhons<asdf"},
{
id: 2,
firstName: "David>tyy",
lastName: "Bowie<wwww"},
{
id: 2,
firstName: "David<test",
lastName: "testqwwe>qewrqwe"}
]),
delay: 3
},
success: function(data, textStatus, jqXHR) {
// since we are using jQuery, you don't need to parse response
drawTable(data);
}
});

function drawTable(data) {
for (var i = 0; i < data.length; i++) {
drawRow(data[i]);
}
}

function drawRow(rowData) {
var row = $("<tr />")
$("#personDataTable").append(row); //this will append tr element to table... keep its reference for a while since we will add cels into it
row.append($("<td>" + rowData.id + "</td>"));
row.append($("<td>" + rowData.firstName + "</td>"));
row.append($("<td>" + rowData.lastName + "</td>"));
}

JSFiddle

谢谢

最佳答案

<强> Demo

你可以使用

row.append($("<td>" + rowData.lastName.replace("<","&lt") + "</td>"));

代替

row.append($("<td>" + rowData.lastName + "</td>"));

If you use the less than (<) or greater than (>) signs in your text, the browser might mix them with tags.Character entities are used to display reserved characters in HTML.

<强> Manual

关于jquery - jQuery 数据表中不允许使用小于 (<) 符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28577589/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com