是否可以使用jquery和split方法跳过html标签? [英] Is it possible to skip html tags using jquery and split method?

查看:74
本文介绍了是否可以使用jquery和split方法跳过html标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 split 将javascript生成的文本插入到html文本中,但我需要跳过像html标签这样的部分< / >< \> ,所以经过大量搜索后,我发现了这一点:

  var = var.replace(/< \ /?[\\\#' -  =:; {},。\\ n] + \ /?> / g,'\ n' ); 

问题是它没有跳过标签或该变量中指定的任何内容,它实际上用空格替换标签。



我需要实现的是,例如:



< script>随机javascript< / script>< p>我的文字< / p> ---需要插入的新文字---



目前正在发生什么:



我的文字< / p> ---需要的新文字插入---



正如你所看到的,第一个标签(javascript)没有被遮挡,它们被剥离了。 / p>

什么方法我应该使用而不是替换?

解决方案


将javascript生成的文本插入到html文本中



我需要实现的是,例如:



< script> random javascript< / script>< p> my text< / p> ---新文本>需要插入---


尝试使用 document.createTextNode() .insertAfter()



  var text = document .createTextNode(---需要插入的新文本---); $(text).insertAfter(p:contains(my text)) 

 < script src =https://ajax.googleapis.com/ajax/libs/jquery/1.11.1 /jquery.min.js\"></script><script>var str =random javascript< / script>< p> my text< / p>  


I'm using split to insert javascript generated text into a html text, but I need to skip the parts like html tags </><\>, so after searching a lot, I found this:

var = var.replace(/<\/?[\w#"'-=:; {},.\r\n]+\/?>/g, '\n');

the problem is that it doesn't skip the tags or anything that is specified in that variable, it actually replaces the tags with a blank space.

What I need to achieve is, example:

<script>random javascript</script><p>my text</p> --- New text that needs to be inserted ---

What's currently happening:

my text</p> --- New text that needs to be inserted ---

As you can see, the first tags (The javascript) are not being skiped, they are being stripped.

What method should I use instead of replace?

解决方案

to insert javascript generated text into a html text

What I need to achieve is, example:

<script>random javascript</script><p>my text</p> --- New text that > needs to be inserted ---

Try using document.createTextNode() , .insertAfter()

var text = document.createTextNode("--- New text that needs to be inserted ---");
$(text).insertAfter("p:contains(my text)")

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<script>var str = "random javascript"</script><p>my text</p>

这篇关于是否可以使用jquery和split方法跳过html标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆