JavaScript 文本追加
我不知道如何描述这一点,但我相信如果您访问此链接,您就会明白。 http://jsfiddle.net/pahnin/yN3xf/
我想使用 javascript 将文本附加到 ap 元素我成功了,但是如果文本包含像 这样的标签,则标签将按原样显示。
我应该添加代码来检测 html 元素还是可以通过其他方式完成? 如果我添加检测字体标签的代码如何将标签添加回文本?
I dont know how to describe this but I'm sure you'll understand if you visit this link.
http://jsfiddle.net/pahnin/yN3xf/
I want to append text to a p element with javascript and I'm sucessful, but if the text contains a tag like <font>
the tag is displayed as it is.
Should I add code to detect the html elements or it can be done any other means?
if I do add code which detect font tag how to add the tag back to the text??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您只需将
var textcopied = $('.welcome').html();
替换为var textcopied = $('.welcome').text();
即可提取不包含任何 HTML 标签的文本。但是,当然,你根本不会拿回你的标签。更新:一种稍微不同的方法使用 jQuery 动画将整个标题平滑地滑入视图:
http://jsfiddle.net/mblase75/yN3xf/16/
You could simply replace
var textcopied = $('.welcome').html();
withvar textcopied = $('.welcome').text();
to extract the text without any HTML tags included. But then, of course, you won't get your tags back at all.Update: A somewhat different approach uses jQuery animations to slide the entire title into view smoothly:
http://jsfiddle.net/mblase75/yN3xf/16/
你可以做这样的事情。写完所有文本后,将
welcome
的整个 html 替换为原始文本。这不是我承认的最好的。http://jsfiddle.net/yN3xf/13/
更新
这应该给出通过不同的方式达到你想要的效果。它在原始文本之上有一个 div,它会缓慢地显示文本,看起来像是正在打印出来的。
示例
http://jsfiddle.net/guanome/LrbVy/
html
javascript
css
UPDATE 2
通过此更改,覆盖层将动态添加到欢迎消息中,不必设置宽度,并且可以轻松地处理多行。
http://jsfiddle.net/guanome/LrbVy/4/
html
JavaScript
You could do something like this. Once all the text has been written out, then you replace the whole html of
welcome
with the original text. It's not the best I admit.http://jsfiddle.net/yN3xf/13/
UPDATE
This should give you the desired effect through different means. It has a div on top of the original text, and it slow reveals the text, which looks like it is being typed out.
Example
http://jsfiddle.net/guanome/LrbVy/
html
javascript
css
UPDATE 2
With this change, the overlay will be added dynamically to the welcome message, the width doesn't have to be set, and it will work with multiple lines easily.
http://jsfiddle.net/guanome/LrbVy/4/
html
javascript
您还可以通过迭代根元素的
contents()
并逐一单独输出每个子节点来实现此目的。在处理每个
contents
元素时,如果它是一个文本节点,那么超时输出所有字符就足够了。如果它不是文本节点,则克隆该节点并将其附加到目标元素。其中的所有字符都可以以相同的方式附加。在这里查看它的工作原理: http://jsfiddle.net/yN3xf/36/
此示例可以进行修改允许嵌套标签,例如:
You can also achieve this by iterating over the root element's
contents()
, and outputting individually each of the children nodes, one by one.When treating each of
contents
elements, if it is a text node, it is enough to output all characters with a timeout. If it is not a text node, clone the node and append it to the target element. All characters inside it can be appended in the same way.See it working here: http://jsfiddle.net/yN3xf/36/
This sample could be adapted to allow nested tags, for instance:
尝试将: 替换
为:
在代码的开头,输入:
它可以工作,但看起来不太好:P
Try replacing:
with:
And at the begening of the code, put this:
It works, but it doesn't look very good :P