如何在返回值中换行?或者返回 HTML 的更好方法
How to line break in a return value? Or a better way of returning HTML?
我正在尝试在我的showPhoto()函数中创建换行符,但似乎没有任何效果。我试过使用 br,尝试使用 html() 但仍然没有让步。我的 Javascript 代码中的注释显示了我正在谈论的返回行。如果有更好的方法可以达到我想要的结果??
任何帮助,不胜感激。谢谢
.JS:
function photo(theCountry, theLocation, theCamera, theDescription) {
this.photoCountry = theCountry;
this.photoLocation = theLocation;
this.photoCamera = theCamera;
this.photoDescription = theDescription;
this.showPhoto = function() {
// How do i create line breaks here?? I want each sub heading on a different line.
return 'Country: ' + this.photoCountry + ' Location: ' + this.photoLocation + ' Shot With: ' + this.photoCamera + ' Description: ' + this.photoDescription;
}
}
var photo1 = new photo('New Zealand', 'Queenstown', 'Canon 550D', '...');
var photo2 = new photo('Singapore', 'Skyline', 'Canon 550D', '....');
var photo3 = new photo('Canada', 'Vancouver', 'Canon 550D', '...');
var photo4 = new photo('New Zealand', 'Lake Wanaka', 'Canon 550D', '....');
var photo5 = new photo('Australia', 'Mornington Peninsula', 'Canon 550D', '...')
$('.photoImg').click(function() {
var sourceImage = document.createElement('img');
sourceImage.className = 'photoFocus';
sourceImage.src = this.src;
var theDiv = document.getElementById('selectionContainer');
$(theDiv).html(sourceImage);
var textDiv = document.createElement('div');
textDiv.setAttribute('id', 'textContainer');
var textNodeArray = {'photo1': photo1, 'photo2': photo2, 'photo3': photo3, 'photo4': photo4, 'photo5': photo5}
$('#textContainer').html(document.createTextNode(textNodeArray[this.id].showPhoto()));
});
你可以
像这样使用CSS
li{
float:left;
padding:5px; /* or anything you need */
margin-top:10px; /* or anything you need */
}
这是你的 HTML
<ul>
<li> <img src='images/newzealand.jpg' class='photoImg' id='photo1'> </li>
<li> <img src='images/singapore.jpg' class='photoImg' id='photo2'> </li>
<li> <img src='images/vancouver.jpg' class='photoImg' id='photo3'> </li>
<li> <img src='images/newzealand2.jpg' class='photoImg' id='photo4'> </li>
<li> <img src='images/mornington.jpg' class='photoImg' id='photo5'> </li>
<li> <img src='images/newzealand1.jpg' class='photoImg' id='photo6'> </li>
<li> <img src='images/newzealand3.jpg' class='photoImg' id='photo7'> </li>
</ul>
你不能在 TextNode(由 createTextNode 创建)中添加换行符。
看到这个 :
Javascript,尝试在创建文本节点方法中添加换行符
一个解决方案可能是让你的showPhoto
函数返回不同行的文本列表,并创建多个由"br"节点分隔的 TextNode。
本着以下精神的东西:
this.showPhoto = function() {
// How do i create line breaks here?? I want each sub heading on a different line.
return ["Country : " + this.photoCountry,
"Location : " + this.photoLocation, ...]
}
然后(可能在行上使用迭代器,任何可能包装 DOM 元素以取悦 jquery)
var lines = textNodeArray[this.id].showPhoto())
$('#textContainer').append(document.createTextNode(lines[0]);
$('#textContainer').append(document.createElement("br"));
$('#textContainer').append(document.createTextNode(lines[1]);
..
在输出中使用 <br>
标记(您将将其视为 HTML 片段):
return 'Country: ' + this.photoCountry + '<br>Location: ' + this.photoLocation + '<br>Shot With: ' + this.photoCamera + '<br>Description: ' + this.photoDescription;
当你使用 jQuery 时,你可以直接输出你的 showPhoto
方法返回的 HTML:
$('#textContainer').html(textNodeArray[this.id].showPhoto());
jQUery将解析HTML字符串并为您处理实际的DOM节点创建。
相关文章:
- JS验证ajax返回的html中的表单数据
- php函数的ajax html$_POST值返回null
- Ajax调用返回当前html页面,而不是请求的文件
- 当使用ajax并将html数据保存为对象时,收听浏览器返回按钮.好的或坏的
- 统计ajax返回数据中的html元素
- HTML如何根据javascript函数的返回值限制文本输入
- 从JavaScript中正确返回html中的特殊字符
- 为什么我得到的是返回的[object HTML ParagraphElement]而不是我的号码
- 在ajax html返回中调用ajax会阻止第一个ajax脚本进一步工作
- jQuery filter 和 html 返回奇怪的结果
- 使用 Javascript 从 JSON 文件向 HTML 返回单个值
- JavaScript-使用selenium-webdriver将内部html返回给变量
- html返回中的Phantomjs编码问题
- 如何从HTML返回标签中获取值?
- 改变Html返回函数的元素
- 从用户处获取HTML输入,并将修改后的HTML返回给用户
- 如何获得html返回通过javascript跨域
- 用Jquery动态追加HTML返回[Object Object]
- 用OpenCPU显示R函数的html返回值
- 我如何执行内联javascript在HTML返回从ajax调用- JQUERY