只有在父级和子级之间没有文本时,我才需要将所有子内联样式传递给父级。
<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
});
我该怎么办?
答案 0 :(得分:0)
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*
}