如何在脚本上添加新行
How to add a new line on a script
你好,我不知道我的标题是否有帮助,但这是我的问题,我想在JS、CSS、HTML中创建一个类型编写器效果,除了添加一行新的文本外,其他一切都很好。
var str = "<p>I want to put text here then another line under this one</p>",
<!--var str = "<p>text here</p>",--> <!--This is what I tried to do to add a new line-->
i = 0,
isTag,
text;
(function type() {
text = str.slice(0, ++i);
if (text === str) return;
document.getElementById('typewriter').innerHTML = text;
var char = text.slice(-1);
if( char === '<' ) isTag = true;
if( char === '>' ) isTag = false;
if (isTag) return type();
setTimeout(type, 80);
}());
#typewriter {
color: lime;
text-align: center;
}
<div id="typewriter"></div>
var str = "My text'nSome more text";
var stra = str.split("");
var tw = document.getElementById("output");
function type(){
var char = stra.shift();
if (char){
tw.innerHTML += char;
setTimeout(type, 80);
}
}
type();
<pre id="output"></pre>
使用<br />
var str = "<p>I want to put text here<br /> then another line under this one</p>";
另一种可能性是使用span对paragragh元素进行分组,并将span的显示样式属性添加到块中。
window.onload = function () {
var str = "<p><span>I want to put text here then another line under this one</span><span>text here</span></p>";
(function type(isInTagArg, indexArg) {
var index = indexArg || 0;
if (index >= str.length)
return;
var isInTag = isInTagArg || false;
if (isInTag == false) {
if (str.charAt(index) == '<') {
return type(true, index + 1);
} else {
document.getElementById('typewriter').innerHTML = str.substr(0, index + 1);
}
} else {
if (str.charAt(index) == '>') {
return type(false, index + 1);
}
return type(true, index + 1);
}
setTimeout(function() {type(false, index + 1)}, 80);
}());
}
#typewriter {
color: lime;
text-align: center;
}
#typewriter span
{
display: block;
}
<div id="typewriter"></div>
相关文章:
- 如何在脚本上添加新行
- 在不刷新整个网格的情况下,向Asp.net网格视图添加新行
- dhtmlx添加新行并启动单元格编辑器
- 如何在Google Geochart图表的工具提示文本中添加新行
- 如何在javascript中每次添加新行时都增加ordre值
- 两个表添加新行按钮-冲突
- 在Spring.message属性中添加新行时出错
- 使用Javascript在文本区域中添加新行
- 在java脚本的第一行之前添加新行
- sails.js更新或添加新行
- 使用ajax向表中添加新行
- 使用一些默认值在剑道网格中添加新行
- 添加新行后无法“刷新”灯丝组表锯响应表
- JQuery 数据表添加新行,如果该行已经存在,请将其删除并为该 ID 插入新行
- 续集 - 向表添加新行并关联表
- 如何在 jQuery 函数 text() 中添加新行
- 在表格顶部添加新行
- 添加新行动态引导日期时间选择器不起作用
- 如何在不影响 Jquery 数据表中其他行的情况下删除选定的现有数据并添加新行
- 分机.js确认方法添加新行字符