我有以下 html 片段。我试图让用户在文本区域中输入一些 html,然后我将解析他们的样式 block 并用他们的样式做一些事情。如何将 html 字符串从 #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>
最佳答案
下面的代码片段可能更适合您。
$( $("#html").val() ).find('style').each(function(){
// do stuff
});
关于jquery - 如何将 html 从 textarea 传递到 jQuery?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3761827/