到目前为止,我知道如何获得innerHTML,但现在我只想获得指定的标记。假设我只想要那些包含test-class的标签行。
<div id="parent">
<div class="test-class"> some texts </div>
<div class="class_1"> some text </div>
</div>
$(functio() {
$('#parent').html();
}); 但我的预期结果应该是<div class="test-class"> some texts </div>.,请告诉我任何想法。
提前谢谢。编辑@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>发布于 2011-09-08 09:28:05
// 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();
}); 发布于 2011-09-08 09:24:48
function() {
$('#parent').find(".test-class").html();
}); 发布于 2011-09-08 09:28:32
你可以这样做-
$('#parent .test-class').clone().wrap('<div></div>').parent().html()这将从test-class div获得整个.clone().wrap('<div></div>').parent().html(),.clone().wrap('<div></div>').parent().html()是为了确保您得到完整的HTML标记。如果你这么做了-
$('#parent .test-class').html()只返回some texts。
工作演示- http://jsfiddle.net/KTHDZ/
https://stackoverflow.com/questions/7345834
复制相似问题