Javascript 使用JQuery创建HTML父标记

Javascript 使用JQuery创建HTML父标记,javascript,jquery,html,image,Javascript,Jquery,Html,Image,我正在尝试将父标记添加到我的 这是一个段落标签 这是第一段标签 这是第二段标签 $(“表格”)。提交(功能(事件){ event.preventDefault(); var textarea=$(“textarea”).val(); var Div=document.createElement(“Div”); $(Div).html(textarea); var divtext=$(Div.html(); $(Div).find(“p:has(img)”).each(函数(i){ var

我正在尝试将父
标记添加到我的

这是一个段落标签

这是第一段标签

这是第二段标签

$(“表格”)。提交(功能(事件){ event.preventDefault(); var textarea=$(“textarea”).val(); var Div=document.createElement(“Div”); $(Div).html(textarea); var divtext=$(Div.html(); $(Div).find(“p:has(img)”).each(函数(i){ var images=$(this.html(); var d=document.createElement(“div”); $(this.html(“”); $(d).附于(本); $(d).附加(图像); }); $(“textarea”).val($(Div.html()); });
这里我需要像这样的输出

<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8"> 
        <title>submit demo</title> 
        <script src="D:\jquery\jquery.js"></script>
    </head>
    <body> 
        <form> 
            <div>   
                <textarea rows="50" cols="200"><p>This is a paragraph Tag</p>
                <p>
                    <img src='C:\Users\Public\Pictures\Sample Pictures\Desert.jpg'></img>this is a First paragraph tag
                </p>
                <p>
                    this is a second paragraph tag
                </p>
                <input type="submit"> 
            </div>
        </form>
        <span></span>
        <script>
            $("form").submit(function(event) {  
                event.preventDefault();
                var textarea = $("textarea").val();
                var Div = document.createElement("div");
                $(Div).html(textarea);
                var divtext = $(Div).html();

                $(Div).find("p:has(img)").each(function(i){
                    var images = $(this).html();
                    var d=document.createElement("div");
                    $(this).html("");
                    $(d).appendTo(this);
                    $(d).append(images);
                });

                $("textarea").val($(Div).html());
            });
        </script>
    </body>
</html>

这是第二段标签

只有

这是第二段标签


请帮帮我。提前感谢。

首先,您用于
img
标记的HTML无效。试试这个:

<P>
    <DIV>
        <IMG src="C:\Users\Public\Pictures\Sample Pictures\Desert.jpg"></IMG>
    </DIV>
    this is a second paragraph tag
</P>

其次,您需要使用
wrap(“”)在他们身上

<P>
    <DIV>
        <IMG src="C:\Users\Public\Pictures\Sample Pictures\Desert.jpg"></IMG>
        this is a second paragraph tag
    </DIV>
</P>
$('divimg').wrap('');
使用
.wrap()
,我们可以在内部
元素
周围插入HTML结构。新的
元素
是动态创建的,并添加到
DOM
。结果是一个新的
包裹在每个
匹配元素的周围

<img src='C:\Users\Public\Pictures\Sample Pictures\Desert.jpg' />
$(“img”).wrap(“”);
输出

$('div img').wrap('<div />');

您的
未关闭

解决方案是将您的img标签包装在Div中。这非常简单,只需一行JQuery

$( "img" ).wrap( "<div class='myClass'></div>" );
$('img')。换行(“”);

<div class='myClass'><img src='C:\Users\Public\Pictures\Sample Pictures\Desert.jpg'></img></div>
$('img')。换行(“”);
如果需要,您可以向div添加属性。

$('img').wrap(“”)
$('img').wrap('<div />');
$('img').wrap('<div></div>');