嵌套标签跨度
问题描述:
只有在父和子之间没有文本时,我需要将所有子内联样式传递给父级。嵌套标签跨度
<span style="text from style"> <!-- this don't recive the childs' style because there are "cccc" -->
cccc
<span style="text from style"> <!-- this span recive the style of his child because there aren't plain text between them -->
<span style="text from style">
bbb
</span>
</span>
</span>
我得到过孩子样式父,但我不能检查的标签孩子
前后也有文字,这是我的脚本:
currentElement.find('span').each(function(){
var $padreSpan = jQuery(this); //get the parent span
jQuery(this).find('span').each(function(){ //pass by all span child
var styleChildren = jQuery(this).attr('style'); //get them style
$padreSpan.attr('style', $padreSpan.attr('style')+ ";" + styleChildren+";"); //set style child to parent
});
$padreSpan.html(
$padreSpan.html().replace(/<span\b[^>]*>/gi,"").replace(/<\/span>/gi,"")
); //clear child spans
});
我怎么能做?
答
var parent=$('span');
var children=$(parent).children();
如果删除所有子节点,剩下的就是文本。因此:
var backup=$(children).remove();
var text=$(parent).text().trim();
var length=text.length;
if(length>0){
//it means there is some text here.
//all the removed node are there in the backup
//re insert it anyway you like*
}