gpt4 book ai didi

javascript - 无法在javascript中获得 "\n"的效果

转载 作者:行者123 更新时间:2023-12-02 14:06:33 25 4
gpt4 key购买 nike

function myMap() {
var addrlist = document.getElementById("addresslist");

var jsaddr = "Address Line1 <br /> Address Line 2 <br /> Address Line 3";
var addr = jsaddr.split("<br />");
//alert(addr);
var addrfinal = "";
for(var j=0;j<addr.length;j++)
{
addrfinal = addrfinal + addr[j] + "\n";
}
alert(addrfinal);

var traddr = document.createElement("tr");
var tdaddr = document.createElement("td");
var tdaddress = document.createTextNode(addrfinal);
tdaddr.appendChild(tdaddress);
traddr.appendChild(tdaddr);
addrlist.appendChild(traddr);
}
<table id="addresslist">
</table>
<input type="button" value="Click" onclick="myMap()" />

我想逐行将地址添加到表中。但是当我警告字符串时它会得到“\n”的效果,但是在字符串中我无法得到“\n”的效果

最佳答案

I want to add address to table line by line.

在这种情况下,你只是让事情变得复杂了。您所需要的只是在 td 元素上使用innerHTML 属性并分配初始字符串。

tdaddr.innerHTML = jsaddr;

检查下面的示例。

function myMap() {
var addrlist = document.getElementById("addresslist");

var jsaddr = "Address Line1 <br /> Address Line 2 <br /> Address Line 3";

var traddr = document.createElement("tr");
var tdaddr = document.createElement("td");

tdaddr.innerHTML = jsaddr; // this is the change
traddr.appendChild(tdaddr);
addrlist.appendChild(traddr);
}
<table id="addresslist">
</table>
<input type="button" value="Click" onclick="myMap()" />

关于javascript - 无法在javascript中获得 "\n"的效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40013798/

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