1
我有以下的html代码片段。我试图让用户在textarea中输入一些html,然后我将解析出他们的样式块,并用他们的样式做一些事情。我怎样才能将#html textarea中的html字符串传递给jQuery,以便我可以解析它?如何将HTML从textarea传递给jQuery?
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="script/jquery-1.3.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
function ParseIt() {
alert($("#html").val());
$("style", $("#html")).each(function(){ alert($(this).text());});
}
</script>
</head>
<body>
<textarea id="html" name="html" class="email_body " rows="25" cols="100"></textarea>
<br />
<input type="button" value="Parse It" onclick="ParseIt();" />
</body>
</html>
也尝试过这个功能,没有运气:
<script type="text/javascript">
function ParseIt() {
alert('parsing');
$($("#html").val()).find('style').each(function() {
alert('found style');
alert($(this).text());
});
}
</script>
试过这个和一些类似的变化,但它似乎并没有工作。试图寻找只是一个div或任何东西,它似乎从未使用非常简单的html示例工作。 – brendan 2010-09-21 15:40:55
似乎在这里工作很好 - http://jsfiddle.net/eMbDP/1/ – BBonifield 2010-09-21 15:55:03
感谢您的跟进!你是对的 - 它创建jquery对象,但我似乎无法找到任何样式元素 - 请参阅http://jsfiddle.net/eMbDP/5/ – brendan 2010-09-21 16:05:15