PHP | DOMElement removeAttribute()函数
DOMElement::removeAttribute()函数是PHP中的一个内置函数,用于从元素中删除具有特定名称的属性。
句法:
bool DOMElement::removeAttribute( string $name )
参数:此函数接受一个参数$name ,它包含属性的名称。
返回值:此函数在成功时返回 TRUE,在失败时返回 FALSE。
例外:如果节点是只读的,此函数会抛出 DOM_NO_MODIFICATION_ALLOWED_ERR。
下面的例子说明了PHP中的DOMElement::removeAttribute()函数:
示例 1:
loadXML("
Geeksforgeeks
Second heading
");
// Get the elements
$node = $dom->getElementsByTagName('h1')[0];
echo "Before the removal of attributes:
";
// Get the attribute name and value
$attribute = $node->attributes->item(0);
$attribute_name = $attribute->name;
$attribute_value = $attribute->value;
echo $attribute_name . ' => ';
echo $attribute_value;
// Remove the id attribute
$node->removeAttribute('id');
echo "
After the removal of attributes:
";
// Get the attribute name and value
$attribute = $node->attributes->item(0);
$attribute_name = $attribute->name . ' => ';
$attribute_value = $attribute->value;
echo $attribute_name;
echo $attribute_value;
?>
输出:
Before the removal of attributes:
id => my_id
After the removal of attributes:
=> // Empty value means attribute is removed
示例 2:
loadXML("
Geeksforgeeks
Second heading
");
// Get the elements
$node = $dom->getElementsByTagName('h1')[0];
echo "Before the removal of attributes:
";
// Get the attribute count
$attributeCount = $node->attributes->count();
echo 'No of attributes => ' . $attributeCount;
// Remove the id attribute
$node->removeAttribute('id');
echo "
After the removal of attributes:
";
// Get the attribute count
$attributeCount = $node->attributes->count();
echo 'No of attributes => ' . $attributeCount;
?>
输出:
Before the removal of attributes:
No of attributes => 3
After the removal of attributes:
No of attributes => 2
参考: https://www. PHP.net/manual/en/domelement.removeattribute。 PHP