开发者

get only specific innerHtml

开发者 https://www.devze.com 2023-04-03 23:26 出处:网络
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.

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("&lt;div&gt;").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/

0

精彩评论

暂无评论...
验证码 换一张
取 消