本文介绍了嵌套字符串内的 HTML 字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很长很长的 html,需要包含在 Javascript 字符串中,而 Javascript 字符串又包含在 Java 字符串中,如下所示:

I have a long very long html which needs to be enclosed in Javascript string which in turn is enclosed in Java string as follows:

String html = "javascript:var html='...all goes here...';void(0);";

现在写到哪里了...all到这里...,所有的html包括"和'甚至其他特殊字符.我可以用Java的方式跳过它们吗?

Now where is written ...all goes here... , there is all html including " and ' and even other special characters. Can I skip them in the Java way?

推荐答案

在这里,您可以体验多次解释字符串的乐趣.您的 " 引号需要为 Java 转义,但您的 ' 引号需要为 javascript 转义.因此,您的 " 引号可以正常转义,但是当解释 javascript 时,您的 ' 引号需要在它们前面加上 \ 字符,所以你的java字符串中需要一个文字\(或\,一个转义的).因此,如果您将 html 变量设置为 html:

Here you get to the fun of strings interpreted multiple times. your " quotes need to be escaped for java, but your ' quotes need to be escaped for javascript. Thus, your " quotes you can escape normally, but your ' quotes need the \ character to be in front of them when the javascript is interpreted, so you need a literal \ in your java string (or \, an escaped ). thus, if you set your html variable to the html:

<span class="class">Here's Johnny!</span>

您需要:

String html = "javascript:var html='<span class=\"class\">Here\\'s Johnny!</span>';void(0);";

这篇关于嵌套字符串内的 HTML 字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 11:54