2011-02-06 53 views
0

如何去除一些html元素而不里面的内容(YUI2)YUI2-如何删除一些没有内容的html元素? ?

简单的例子,除去span.exclass元件而不含量+插入空格用于保持词语分离:

输入:

<br/>&nbsp;<span class="just"></span> 
<div>&nbsp; 
textetxt <span class="exclass">texttetx</span>texttext 
</div>&nbsp; 
<br/> 
<p>&nbsp;<span class="just"></span> 
textetxt <span class="exclass">texttetx</span>texttext 
</p>&nbsp; 

输出:

<br/>&nbsp;<span class="just"></span> 
<div>&nbsp; 
textetxt texttetx texttext 
</div>&nbsp; 
<br/> 
<p>&nbsp;<span class="just"></span> 
textetxt texttetx texttext 
</p>&nbsp; 

jQuery的应该是,如果我没有错 - 但是在yui2我不知道:

$('span.exclass').each(function(){ 
    $(this).replaceWith($(this).text()); 
}); 

感谢, 优素福

回答

1
var elems = YAHOO.util.Dom.getElementsByClassName('exclass', 'span'); 
for (var el, i = elems.length; i-- && (el = elems[i]);) { 
    el.parentNode.replaceChild(document.createTextNode(el.innerHTML + ' '), el); 
}​