php attr,PHP DOMAttr isId()用法及代码示例

DOMAttr::isId()函数是PHP中的内置函数,用于检查属性是否为已定义的ID。根据DOM标准,它要求属性ID为类型ID。使用此功能之前,您需要使用DOMDocument::validateOnParse()方法验证您的文档。

用法:

bool DOMAttr::isId( void )

参数:此功能不接受任何参数。

返回值:如果该函数包含id属性,则该函数返回TRUE,否则返回FALSE。

下面给出的程序说明了PHP中的DOMAttr::isId()函数:

程序1:

// Create a new DOM Document

$dom = new DOMDocument('1.0', 'iso-8859-1');

// Enable validate on parse

$dom->validateOnParse = true;

// Create a div element

$element = $dom->appendChild(new DOMElement('div'));

// Create a class attribute

$attr = $element->setAttributeNode(

new DOMAttr('class', 'geekforgeeks'));

// Get the attribute

$getattr = $dom->getElementsByTagName('div')

->item(0)->getAttributeNode('class');

// Check if it is id or not

if($getattr->isId()) {

echo 'Yes, this is an id';

} else {

echo 'No, this is not an id';

}

?>

输出:

No, this is not an id

程序2:

// Create a new DOM Document

$dom = new DOMDocument('1.0', 'iso-8859-1');

// Enable validate on parse

$dom->validateOnParse = true;

// Create a div element

$element = $dom->appendChild(new DOMElement('div'));

// Create a id attribute

$attr = $element->setAttributeNode(

new DOMAttr('id', 'mynewid'));

// Set that attribute as id

$element->setIDAttribute('id', true);

// Get the attribute

$getattr = $dom->getElementsByTagName('div')

->item(0)->getAttributeNode('id');

// Check if it is id or not

if($getattr->isId()) {

echo 'Yes, this is an id';

} else {

echo 'No, this is not an id';

}

?>

输出:

Yes, this is a id

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值