get only specific innerHtml
as far i know how to get innerHTML but now i want to get only specified tags. let say i only want those line of tags who contains test-class
.
<div id="parent">
<div class="test-class"> some texts </div>
<div class="class_1"> some text </div>
</div>
$(functio() {
$('#parent').html();
});
But my expected result should be <div class="test-class">
some texts
</div>.
please give me any idea.
Thanks in advance. Edit @Js
<html> <head>
/* css & js file attached */
<script type="text/javascript">
jQuery.fn.outerHTML = function(s) {
return (s) ? this.before(s).remove() :
jQuery("<div>").append(this.eq(0).clone()).html();
}
$(function() {
var outerHTML = $('#parent').find('.test-class').outerHTML();
开发者_高级运维 alert(outerHTML);
});
</script>
</head> <body>
/* the same html code as above */
</body> </html>
// ref: http://www.yelotofu.com/2008/08/jquery-outerhtml/
jQuery.fn.outerHTML = function(s) {
return (s) ? this.before(s).remove() :
jQuery("<div>").append(this.eq(0).clone()).html();
}
// get outer html
$(function() {
var outerHTML = $('#parent').find('.test-class').outerHTML();
});
function() {
$('#parent').find(".test-class").html();
});
You could do something like this -
$('#parent .test-class').clone().wrap('<div></div>').parent().html()
This will get you the entire HTML from the test-class
div, the .clone().wrap('<div></div>').parent().html()
is there to ensure you get the full HTML mark-up. If you just did -
$('#parent .test-class').html()
Only some texts
would be returned.
Working demo - http://jsfiddle.net/KTHDZ/
精彩评论