Jquery return element html instead of content
I have something like this
<ul>
<li class="aclass" id="a">content</li>
<li class="aclass" id="b">content</li>
<li class="aclass" id="c">content</li>
<li class="aclass" id="d">content</li>
<li class="aclass" id="e"&开发者_开发百科gt;content</li>
<li class="aclass" id="f">content</li>
</ul>
I have code like
$(".aclass").live("mousedown",function() {
alert($this.html());
});
This will alert the content, what I would like to do is alert the entire element like
<li class="aclass" id="f">content</li>
I've tried $(this).parent() but that returns the whole UL
alert($(this).clone().wrap('<div/>').parent().html());
alert($('<div/>').append($(this).clone()).html());
http://jsfiddle.net/keegan3d/nfEyQ/
To get the html-code of a DOM-Node you can use the property outerHTML
var html = document.getElementById('someId').outerHTML;
this sadly doesnt work in firefox. But you can use XMLSerializer to achieve the same result. Like this
var elm = document.getElementById('someId');
var xmls = new XMLSerializer();
var html = xmls.serializeToString(elm);
in your jQuery-code this might look something like this:
$(".aclass").live("mousedown",function() {
alert( this.outerHTML || new XMLSerializer().serializeToString(this) );
});
精彩评论