从带有Uniqqe Id的段落数组中添加句子's
Adding Sentences From Paragraph Array With Uniqqe Id's
我正在写一个脚本,它将一本书作为一组句子。我想把每个句子打印出来,然后让每个句子都可以点击。现在,我让它打印每个句子,但当我试图为每个句子添加一个带有它自己唯一id的
元素时,它不起作用。这是我的jsfidde:http://jsfiddle.net/mvvq8L7p/
相关代码片段。
var IdCounter = 0;
for (var i = 0, len = book_sentences.length; i < len; i++) {
IdCounter++;
document.getElementById("pageOfBook").innerHTML = "<p class='sentence' id='sentence#" + IdCounter + "'>" + book_sentences[i] + "</p>";
}
有人能解释为什么会发生这种情况以及如何使其发挥作用吗?提前谢谢,我真的很感激你的帮助!
只需用'.'分隔段落,并使用IDCounter/将其附加到您的div
var book = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
var book_sentences = book.split('.');
IdCounter = 0;
book_sentences.forEach(function(data){
IdCounter = IdCounter + 1;
console.log(data);
document.getElementById("pageOfBook").innerHTML += "<p class='sentence' id='sentence#" + IdCounter + "'>" + data + ".</p>";
});
<div id="pageOfBook"></div>
相关文章:
- 正在添加'X'按钮,在文本字段旁边使用javascript
- 如何在映射数组中添加换行符
- 正在将数据主题添加到所有项目
- ZeroClipboard-在复制之前添加到值
- 我可以在json对象中添加一个函数吗
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 正在SharePoint 2013母版页中添加JQuery移动文件
- 我如何找到一个句子中的所有空格并替换忽略它们
- 如何添加浮动和非浮动,其他
- 添加文字和评论功能更新Div
- 如果使用 lodash 将属性存在于另一个对象中,则向对象添加属性
- 动态地改变“”的URL;添加新项目”;链接使用javascript/jquery
- 在函数中添加数组元素的数值
- 使用jQuery检查提交时添加到句子中的单词
- 需要使用javascript获取输入文本,然后将其添加到句子中
- 如何使用Javascript在给定的句子中找到大写单词并在其前面添加一个字符
- jQuery:如何添加<br/>在不同的字符串中换行以分隔2中的句子
- 从带有Uniqqe Id的段落数组中添加句子's
- Regex用于在特定条件下为新句子添加空格或句号
- 如何为段落中的每个句子添加递增ID