我不确定这个问题有多实用,但我想做的是在字符串中找到 html,然后使用 jquery 将 pre 标签附加到它。假设我有一个带有以下字符串的变量。
var string = "Some normal text <html><body><div> This is the html </div> </body></html>";
字符串中的html是动态的,可以包含任何标签,我想要的是找到html的开始和结束并相应地追加,前置标签。
谢谢
最佳答案
下面的代码几乎可以满足您的需求,但是它不允许 html、body 标签等。但是这些在 pre 标签中是不允许的。
var string = "Some normal text <html><body><div> This is the html </div> </body></html> more text<p>more html content</p>";
var holder = $('<div>').html(string);
holder.children().wrap('<pre>');
//Print result to console
console.log(holder.html());
这里还有一个 jsFiddle:http://jsfiddle.net/evBCm/1/
关于javascript - 使用 Jquery 在字符串中查找 Html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9851364/