我正在尝试创建一个函数(在JavaScript中)通过在每个(例如)300毫秒(在<p>标记中)逐个编写其字母来编写句子.我写过:
var text = ["H", "e", "l", "l", "o", " ", "h", "o", "w", " ", "a", "r", "e", "y", "o", "u", "?"]
function typeText() {
var i = 0;
var interval = setInterval(function () {
var parag = document.getElementById("theParagraph");
var paragOldText = parag.innerText;
parag.innerText = paragOldText + text[i];
i++;
if (text.length == i)
clearInterval(interval);
}, 200)
}Run Code Online (Sandbox Code Playgroud)
<body>
<p id="theParagraph"></p>
<button id="typeButton" onclick="typeText()" style="padding:15px">Start typing the sentence</button>
</body>Run Code Online (Sandbox Code Playgroud)
如您所见,数组中有一些""(空格)字符; 问题是它没有写那些空格,所以句子就像这样:"Hellohowareyou".我该如何解决这个问题?
If I am going to be displaying user-generated input on my site, is it safe enough to just display it by doing Element.innerText = "user input" in javascript, or do I need to additionally filter the input to prevent XSS?
我过去一直使用它,没有任何问题。这次我似乎无法弄清楚出了什么问题;也许我只需要第二双眼睛。
这是我的 HTML 代码:
<form id="myForm">
<table>
<tr>
<th>Product</th>
<th>Price</th>
</tr>
<tr>
<td>2014 HK Army Hardline Paintball Jersey</td>
<td>$89.95</td>
</tr>
<tr>
<td>Planet Eclipse 2013 Distortion Gloves</td>
<td>$34.95</td>
</tr>
<tr>
<td>Eclipse Geo 3.5 Paintball Marker</td>
<td>$1,600.00</td>
</tr>
</table>
<p>How many Paintball Jerseys would you like to order?</p>
<input id="jersey" type="number" value="0">
<p>How many Paintball Gloves would you like to order?</p>
<input id="gloves" type="number" value="0">
<p>How many Paintball Markers would you like to order?</p>
<input id="marker" type="number" value="0"><br>
<input type="button" onclick="submit()" value="Submit …Run Code Online (Sandbox Code Playgroud) 在html中,如何计算innerHTML中显示的真实字符?
例如,在 中<strong>Hi</strong>,字符数应为 2,而不是 19。
我尝试过2种方法均无效:
str.length- 这将包括<>角色。getElementById- 这将包括<>字符:我尝试如下:
elem_output = document.getElementById("example_id");
elem_output.innerText.length;//1st
elem_output.textContent.length;//2nd
Run Code Online (Sandbox Code Playgroud) 在下面的示例中,在contenteditablediv中输入HTML 应该在HTML #html_view中呈现,但不是呈现为HTML,而是呈现为文本.
例如:当输入以下HTML时,它应该在红色容器内呈现"sometext",但字符串按原样传输.
<p style="background-color:red;">sometext</p>
Run Code Online (Sandbox Code Playgroud)
如何将contenteditablediv 的内容呈现为另一个元素中的HTML?
function move_to_the_other_div(el){
document.getElementById('html_view').innerHTML = el.innerHTML;
}Run Code Online (Sandbox Code Playgroud)
#html_view{
width:300px;
overflow:hidden;
border:1px solid gray;
padding:10px;
min-height:50px;
}
#code_view{
width:300px;
border:1px solid gray;
padding:10px;
min-height:50px;
font-size:18px;
}Run Code Online (Sandbox Code Playgroud)
<div id="html_view"></div>
<div contenteditable id="code_view" oninput="move_to_the_other_div(this);" onpaste="move_to_the_other_div(this);"></div>Run Code Online (Sandbox Code Playgroud)
我认为它会是这样的(伪代码):
var pars = new List<string>();
string par;
while (not eof("Platypus.html"))
{
par = getNextParagraph();
pars.Add(par);
}
Run Code Online (Sandbox Code Playgroud)
... getNextParagraph() 查找下一个"<p>"并继续,直到找到"</p>",烧毁其后面的桥梁(“剪切”该段落,以便不会一遍又一遍地找到它)。或者一些这样的。
有人知道如何准确地做到这一点/更好的方法吗?
我尝试使用 Aurelien Souchet 的代码。
我有以下用途:
using HtmlAgilityPack;
using HtmlDocument = System.Windows.Forms.HtmlDocument;
Run Code Online (Sandbox Code Playgroud)
...但是这段代码:
HtmlDocument doc = new HtmlDocument();
Run Code Online (Sandbox Code Playgroud)
是不需要的(“此处无法访问私有构造函数'HtmlDocument' ”)
此外,“doc.LoadHtml()”和“doc.DocumentNode”都给出了旧的“无法解析符号'Bla'”错误消息
好吧,我必须在前面加上“HtmlAgilityPack”。从而消除了含糊的参考。
我想使用JavaScript计算段落中的字符.例:
HTML:
<p id="text">Example</p>
Run Code Online (Sandbox Code Playgroud)
使用Javascript
some code
Run Code Online (Sandbox Code Playgroud)
产量
7.
Run Code Online (Sandbox Code Playgroud)
我想像jQuery代码一样有效:
$("#text").text().length;
Run Code Online (Sandbox Code Playgroud)
但我需要JavaScript一些帮助吗?
我已经试过了
var x = document.getElementById("text").length;
Run Code Online (Sandbox Code Playgroud)
输出:未定义