Javascript返回字符串

时间:2016-04-01 07:59:22

标签: javascript html

我有一个返回所选

的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;
}

任何帮助将不胜感激!

4 个答案:

答案 0 :(得分:1)

如果你想要新的一行......

  • 在您的HTML 代码(或一般字符串)中,使用\n
  • 在您的HTML 输出中,使用<br/>标记
  • 在HTML 代码及其输出中,将它们合并(如<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元素中的每个项目包裹起来,如divpli

然后,您必须确保在插入页面之前未使用jQuery .html()element.innerHTML = "...";等函数转义html。

答案 3 :(得分:0)

另一次尝试,尝试在ph1之类的元素中添加字符串

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>";
}