使用jquery在javascript变量中复制html div

时间:2010-01-19 10:40:59

标签: javascript jquery html

在我的aspx页面中我有一个包含简单html的div,有一些id(比如'datadiv')

使用jquery我想将该div(整个div)的html变为JavaScript变量

怎么做?

谢谢你。

4 个答案:

答案 0 :(得分:7)

var somvar = $("#somediv").html();

答案 1 :(得分:2)

如果你想要等同于Internet Explorer的outerHTML来检索div及其内容,那么扩展JQuery的function from this page应该有所帮助:#

jQuery.fn.outerHTML = function() {
    return $('<div>').append( this.eq(0).clone() ).html();
};

然后致电:

var html = $('#datadiv')。outerHTML();

答案 2 :(得分:0)

            <!DOCTYPE html>
            <html>
            <head>
            <script src="http://code.jquery.com/jquery-latest.js"></script>
            </head>
            <body>

            <b>Hello</b><p>, how are you?</p>

            <script>
            $("b").clone().prependTo("p");
            </script>

            </body>
            </html>
            **Out put:**
            Hello
            Hello, how are you?

答案 3 :(得分:0)

终于搞定了.. !!最简单的方法!!
HTML代码

<div class="extra-div">
    <div class="div-to-pull-in-variable">
     // Your Content Here
    </div>
</div>

JQuery代码

    $(function() {
        var completeDiv = $('.extra-div').html();
        alert(completeDiv); 
       })
    });