欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

jquery outerHTML

shiping1 的头像
jQuery.fn.outerHTML = function(s) {
    return s
        ? this.before(s).remove()
        : jQuery("<p>").append(this.eq(0).clone()).html();
};




$('A').each(function(){
    var s = $(this).clone().wrap('<p>').parent().html();
    console.log(s);
});

.clone().wrap('<p>').parent().html();

// Gives you the DOM element without the outside wrapper you want
$('.classSelector').html()

// Gives you the outside wrapper as well
$('.classSelector')[0].outerHTML


来自  http://stackoverflow.com/questions/2419749/get-selected-elements-outer-html



Create a temporary element, then clone() and append():

$('<div>').append($('#xxx').clone()).html();

var x = $('#xxx').parent().html();
alert(x);

Universal solution:

// no cloning necessary var x = $('#xxx').wrapAll('<div></div>').parent().html(); alert(x);

No siblings solution:

var x = $('#xxx').parent().html(); alert(x);

Universal solution:

// no cloning necessary var x = $('#xxx').wrapAll('<div></div>').parent().html(); alert(x);

If you don't want to add a wrapper, you could just add the code manually, since you know the ID you are targeting:

var myID = "xxx"; var newCode = "<div id='"+myID+"'>"+$("#"+myID).html()+"</div>";

来自 http://stackoverflow.com/questions/5744207/jquery-outer-html

普通分类: