jquery - 如何将 html 从 textarea 传递到 jQuery?

标签 jquery

我有以下 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/

相关文章:

javascript - 使用android移动浏览器时如何获得连续的mousemove事件?

javascript - HTML/CSS/JS 中的几何图案

mysql - 如何终止使用 $.getJSON 方法发出的 MySQL 查询线程

javascript - Data-toggle 和 onclick 不能一起工作,我太新了,无法将@epascarello 的解决方案应用于

asp.net-mvc - 使用 ASP.NET MVC 的 BlueImp jQuery 上传

javascript - 单击提交按钮并将数据插入 php 中的数据库表时如何显示隐藏表?

jquery - 克隆 <select> 并添加删除按钮

javascript - 如何使用 jQuery/Javascript 旋转单个 SVG 元素?

具有重复按钮元素的 JQuery addClass/hasClass

javascript - jQuery 事件处理程序未在 IE 中触发