我有一个返回所选
的JavaScript函数function getage() {
var selectedAge = selectedYear;
var selectedAge = "";
console.log(selectedAge);
for (var key in selectedAge) { // run for every value in selectedAge
console.log(key);
if (selectedBirth.indexOf('20-' + key) > -1){
if (selectedAge[key].length > 0) { // check if the subarray of selectedAge contains a value (if birth has selected age)
selectedAge = selectedAge + age[key] + " selectedAge"; // add the age code as well as 'selected age' to return string
} else {
selectedAge = selectedAge + birth[key]; // adds only birth name
}
selectedAge = selectedAge + "\n" + "\t";
}
}
return selectedAge;
}
任何帮助将不胜感激!
答案 0 :(得分:1)
如果你想要新的一行......
\n
<br/>
标记<br/>\n
)答案 1 :(得分:0)
编辑:
尝试在代码中添加此var breakline = document.createElement("br");
并将其添加到您的selectedString
function getCoverage() {
var selectedMarkers = selectedTowns;
var selectedString = "";
var breakline = document.createElement('br');
selectedString.appendChild(breakline);
console.log(selectedMarkers);
for (var key in selectedMarkers) { // run for every value in selectedMarkers
console.log(key);
if (selectedRegions.indexOf('ZA-' + key) > -1) { // if current province is one of the selected provinces
if (selectedMarkers[key].length > 0) { // check if the subarray of selectedMarkers contains a value (if province has selected town)
selectedString = selectedString + province[key] + " selected towns"; // add the province code as well as 'selected towns' to return string
} else {
selectedString = selectedString + province[key]; // adds only province name (since regional coverage for this province)
}
selectedString = selectedString
}
}
return selectedString;
}
答案 2 :(得分:0)
在html
中,您只需使用<br />
您想要使用\n
的地方,或者将display: block
元素中的每个项目包裹起来,如div
或p
或li
。
然后,您必须确保在插入页面之前未使用jQuery .html()
或element.innerHTML = "...";
等函数转义html。
答案 3 :(得分:0)
另一次尝试,尝试在p
或h1
之类的元素中添加字符串
function getCoverage() {
var selectedMarkers = selectedTowns;
var selectedString = "";
console.log(selectedMarkers);
for (var key in selectedMarkers) { // run for every value in selectedMarkers
console.log(key);
if (selectedRegions.indexOf('ZA-' + key) > -1) { // if current province is one of the selected provinces
if (selectedMarkers[key].length > 0) { // check if the subarray of selectedMarkers contains a value (if province has selected town)
selectedString = selectedString + province[key] + " selected towns"; // add the province code as well as 'selected towns' to return string
} else {
selectedString = selectedString + province[key]; // adds only province name (since regional coverage for this province)
}
selectedString = selectedString;
}
}
return "<p>" + selectedString + "</p>";
}