Php 如何删除DomeElement的style属性的特定样式属性?
我有一个PHP的DomeElement。我想从元素的style属性及其所有子元素中删除一些特定的样式。我知道它应该是递归的 例如,我有:Php 如何删除DomeElement的style属性的特定样式属性?,php,recursion,domdocument,Php,Recursion,Domdocument,我有一个PHP的DomeElement。我想从元素的style属性及其所有子元素中删除一些特定的样式。我知道它应该是递归的 例如,我有: 我删除了top样式。它转换为: 使用JQUERY尝试下面的代码 $( document ).ready(function() { $('a *').css('top',''); // For Remove specific property of child element $('a').css('top',''); // For Remove speci
我删除了top
样式。它转换为:
使用JQUERY尝试下面的代码
$( document ).ready(function() {
$('a *').css('top',''); // For Remove specific property of child element
$('a').css('top',''); // For Remove specific property of element
});
对父节点使用以下函数:
private function prevent_html_style_properties($element){
if($element->nodeType != XML_ELEMENT_NODE)
return $element;
$element->removeAttribute('class');
if($element->hasAttribute('style')){
$style = $element->getAttribute('style');
$existingDeleteAttrList = array();
$attrList = [
'height',
'position',
'top'];
$style_parts = explode(';', $style);
foreach($attrList as $attr){
if(strpos($style, $attr) !== false)
$existingDeleteAttrList[] = $attr;
}
$new_attr_value = '';
foreach($style_parts as $style_part){
$attr_is_safe = true;
foreach($existingDeleteAttrList as $attr){
if(strpos($style_part, $attr) !== false) {
$attr_is_safe = false;
break;
}
}
if($attr_is_safe)
$new_attr_value .= $style_part . ';';
}
$element->setAttribute('style', $new_attr_value);
}
$children = $element->childNodes;
foreach ($children as $child)
{
$element->replaceChild($this->prevent_html_tag_styles($child), $child);
}
return $element;
}
用法:
$element = $this->prevent_html_style_properties($element);
一些解释:
- 有些子项是简单文本,没有任何属性。所以我们必须检查它是否是
XML\u元素\u节点
- 它使用
修复其直接子级,依此类推replaceChild