Get selected element's outer HTML

前端 未结 30 2451
礼貌的吻别
礼貌的吻别 2020-11-21 04:50

I\'m trying to get the HTML of a selected object with jQuery. I am aware of the .html() function; the issue is that I need the HTML including the selected obje

相关标签:
30条回答
  • 2020-11-21 05:09
    $("#myTable").parent().html();
    

    Perhaps I'm not understanding your question properly, but this will get the selected element's parent element's html.

    Is that what you're after?

    0 讨论(0)
  • 2020-11-21 05:10

    To make a FULL jQuery plugin as .outerHTML, add the following script to any js file and include after jQuery in your header:

    update New version has better control as well as a more jQuery Selector friendly service! :)

    ;(function($) {
        $.extend({
            outerHTML: function() {
                var $ele = arguments[0],
                    args = Array.prototype.slice.call(arguments, 1)
                if ($ele && !($ele instanceof jQuery) && (typeof $ele == 'string' || $ele instanceof HTMLCollection || $ele instanceof Array)) $ele = $($ele);
                if ($ele.length) {
                    if ($ele.length == 1) return $ele[0].outerHTML;
                    else return $.map($("div"), function(ele,i) { return ele.outerHTML; });
                }
                throw new Error("Invalid Selector");
            }
        })
        $.fn.extend({
            outerHTML: function() {
                var args = [this];
                if (arguments.length) for (x in arguments) args.push(arguments[x]);
                return $.outerHTML.apply($, args);
            }
        });
    })(jQuery);
    

    This will allow you to not only get the outerHTML of one element, but even get an Array return of multiple elements at once! and can be used in both jQuery standard styles as such:

    $.outerHTML($("#eleID")); // will return outerHTML of that element and is 
    // same as
    $("#eleID").outerHTML();
    // or
    $.outerHTML("#eleID");
    // or
    $.outerHTML(document.getElementById("eleID"));
    

    For multiple elements

    $("#firstEle, .someElesByClassname, tag").outerHTML();
    

    Snippet Examples:

    console.log('$.outerHTML($("#eleID"))'+"\t", $.outerHTML($("#eleID"))); 
    console.log('$("#eleID").outerHTML()'+"\t\t", $("#eleID").outerHTML());
    console.log('$("#firstEle, .someElesByClassname, tag").outerHTML()'+"\t", $("#firstEle, .someElesByClassname, tag").outerHTML());
    
    var checkThisOut = $("div").outerHTML();
    console.log('var checkThisOut = $("div").outerHTML();'+"\t\t", checkThisOut);
    $.each(checkThisOut, function(i, str){ $("div").eq(i).text("My outerHTML Was: " + str); });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="https://rawgit.com/JDMcKinstry/ce699e82c7e07d02bae82e642fb4275f/raw/deabd0663adf0d12f389ddc03786468af4033ad2/jQuery.outerHTML.js"></script>
    <div id="eleID">This will</div>
    <div id="firstEle">be Replaced</div>
    <div class="someElesByClassname">At RunTime</div>
    <h3><tag>Open Console to see results</tag></h3>

    0 讨论(0)
  • 2020-11-21 05:11

    You can also use get (Retrieve the DOM elements matched by the jQuery object.).

    e.g:

    $('div').get(0).outerHTML;//return "<div></div>"
    

    As extension method :

    jQuery.fn.outerHTML = function () {
      return this.get().map(function (v) {
        return v.outerHTML
      }).join()
    };
    

    Or

    jQuery.fn.outerHTML = function () {
      return $.map(this.get(), function (v) {
        return v.outerHTML
      }).join()
    };
    

    Multiple choice and return the outer html of all matched elements.

    $('input').outerHTML()
    

    return:

    '<input id="input1" type="text"><input id="input2" type="text">'
    
    0 讨论(0)
  • 2020-11-21 05:11

    $.html = el => $("<div>"+el+"</div>").html().trim();

    0 讨论(0)
  • 2020-11-21 05:12

    node.cloneNode() hardly seems like a hack. You can clone the node and append it to any desired parent element, and also manipulate it by manipulating individual properties, rather than having to e.g. run regular expressions on it, or add it in to the DOM, then manipulate it afterwords.

    That said, you could also iterate over the attributes of the element to construct an HTML string representation of it. It seems likely this is how any outerHTML function would be implemented were jQuery to add one.

    0 讨论(0)
  • 2020-11-21 05:13

    you can also just do it this way

    document.getElementById(id).outerHTML
    

    where id is the id of the element that you are looking for

    0 讨论(0)
提交回复
热议问题