PHP | DOMNode hasAttributes()函数
DOMNode::hasAttributes()函数是PHP中的一个内置函数,用于检查节点是否具有属性。
句法:
bool DOMNode::hasAttributes( void )
参数:此函数不接受任何参数。
返回值:此函数在成功时返回 TRUE,在失败时返回 FALSE。
下面的例子说明了PHP中的DOMNode::hasAttributes()函数:
示例 1:
createElement('p',
'This is the paragraph element!');
// Set the attribute
$element->setAttribute('id', 'my_id');
// Append the child
$dom->appendChild($element);
// Get the elements
$nodeList = $dom->getElementsByTagName('p');
foreach ($nodeList as $node) {
if($node->hasAttribute('id')) {
echo "Yes, id attribute is there.";
}
}
?>
输出:
Yes, id attribute is there.
示例 2:
createElement('p',
'This is the paragraph element!');
// Set the attribute
$element->setAttribute('class', 'my_class');
// Append the child
$dom->appendChild($element);
// Get the elements
$nodeList = $dom->getElementsByTagName('p');
foreach ($nodeList as $node) {
if(!$node->hasAttribute('id')) {
echo "No, id attribute is not there.";
}
}
?>
输出:
No, id attribute is not there.
参考: https://www. PHP.net/manual/en/domnode.hasattributes。 PHP