PHP 8.4.2 Released!

DOMElement::setAttributeNode

(PHP 5, PHP 7, PHP 8)

DOMElement::setAttributeNodeAdds new attribute node to element

说明

public DOMElement::setAttributeNode(DOMAttr $attr): DOMAttr|null|false

Adds new attribute node attr to element. If an attribute with the same name already exists on the element, that attribute is replaced by attr.

参数

attr

The attribute node.

返回值

Returns the old attribute if it has been replaced or null if there was no old attribute. If a DOM_WRONG_DOCUMENT_ERR error is raised, and strictErrorChecking is false, false is returned.

错误/异常

DOM_WRONG_DOCUMENT_ERR

Raised if attr belongs to a different document than the element.

参见

添加备注

用户贡献的备注 1 note

up
-1
karvjorm at users.sourceforge.net
17 years ago
$dom = new DomDocument('1.0','iso-8859-15');

$ht_ml = $dom->appendChild($dom->createElement('html'));

$he_ad = $ht_ml->appendChild($dom->createElement('head'));

$tit_le= $he_ad->appendChild($dom->createElement('title'));
$tit_le->appendChild($dom->createTextNode('DOMAttr test'));

$me_ta = $he_ad->appendChild(new DOMElement('meta'));
$me_ta->setAttributeNode(new DOMAttr('name', 'Description'));
$me_ta->setAttributeNode(new DOMAttr('content', 'example'));

$me_ta = $he_ad->appendChild(new DOMElement('meta'));
$me_ta->setAttributeNode(new DOMAttr('name', 'Author'));
$me_ta->setAttributeNode(new DOMAttr('content', 'karvjorm'));

Result:

<?xml version="1.0" encoding="iso-8859-15"?>
<html>
<head>
<title>DOMAttr test</title>
<meta name="Description" content="example"/>
<meta name="Author" content="karvjorm"/>
</head>
To Top