I have a div that contains an unfor开发者_开发问答matted text and two divs. If you ask why the text isn't formatted it's because it's returned by a plugin through the API so I can't really control it. The markup looks like so:
<div id="parentdiv">
<div class="childdiv"></div>
<div class="childdiv"></div>
unformattedtext
</div>
How do I get rid of "unformattedtext"? If possible I would like to do it with CSS but if not it's okay with jQuery.
This removes text-nodes which are direct childs of #parentdiv
:
$("#parentdiv").contents().filter(function(){ return this.nodeType == 3; }).remove();
$('#parentdiv').contents(':not(*)').remove();
You could put unformattedtext
into a div/a/p/span/font with display:inline
, so as not to break formattion.
<div id="parentdiv">
<div class="childdiv"></div>
<div class="childdiv"></div>
<div style="display:inline">unformattedtext</div>
</div>
<script type="text/javascript">
$("#parentdiv :not(.childdiv)").hide(); //Hide whatever is not .childdiv
</script>
JSFiddle: http://jsfiddle.net/kongr45gpen/EBL4G/
精彩评论