Javascript 嵌套标记跨越
只有在父级和子级之间没有文本时,我才需要将所有子级内联样式从传递到父级Javascript 嵌套标记跨越,javascript,jquery,html,nested,inline-styles,Javascript,Jquery,Html,Nested,Inline Styles,只有在父级和子级之间没有文本时,我才需要将所有子级内联样式从传递到父级 <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 c
<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>
中交
bbb
我已将子样式传递给父级,但我无法检查标记子级前后是否有文本
这是我的剧本:
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
});
currentElement.find('span')。每个(函数(){
var$padreSpan=jQuery(this);//获取父范围
jQuery(this).find('span').each(function(){//传递所有span子级
var styleChildren=jQuery(this).attr('style');//获取样式
$padreSpan.attr('style',$padreSpan.attr('style')+“;“+styleChildren+”;”;//将样式子项设置为父项
});
$padreSpan.html(
$padreSpan.html().replace(//*>/gi,“”)。replace(//gi,“”)
);//清除子跨度
});
我该怎么办
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*
}