从包含HTML的JavaScript变量中使用jQuery获取选择器(.html())的html内容 [英] Get html content of a selector ( .html() ) with jQuery from a JavaScript variable that contains HTML

查看:97
本文介绍了从包含HTML的JavaScript变量中使用jQuery获取选择器(.html())的html内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个javascript变量,其中包含html代码的一部分. 我需要在html代码的这一部分中获取div html内容. 我该怎么做 ? 这是一个例子:

I have a javascript variable that contains a part of html code. I need to get in this part of html code a div html content. How can i do it ? This is an example:

var code = '<style type="text/css">
#example{
border:1px;
font-size:20px;
}
</style>
<div id="ex"> Some Content </div>
<div id="ex2"> Some Content <span> Another Content</span></div>
<div id="my_code">This Is My Code.</div><div id="ex3> Etc Etc </div>';

我想用Jquery .html();来获取div"my_code"的内容.

I'd like get content of div "my_code" with Jquery .html();

我该怎么办?

谢谢

推荐答案

code变量,它只是文档的字符串.如果您已在体内解析了此HTML代码,则可以使用$('#my_code'),否则它仍然只是一个字符串,所以..那是另一回事了.

code variable it's just a string for your document. If you have parsed this HTML code inside the body then you can use $('#my_code'), otherwise it's still just a string so.. that's another story.

在此处查看其他故事: http://jsfiddle.net/NSCQh/1/

Check the other story here: http://jsfiddle.net/NSCQh/1/

这篇关于从包含HTML的JavaScript变量中使用jQuery获取选择器(.html())的html内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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