我正在尝试通过 id 获取元素。但我并不成功。为什么以下无法找到具有我给定 ID 的元素?
我已经设置了一个测试用例:
<?php
$m_oDom = new DOMDocument( '1.0', 'UTF-8' );
$m_oDom->formatOutput = true;
$m_oDom->preserveWhiteSpace = false;
$m_oDom->validateOnParse = true;
$strId = "abc";
$oElement = $m_oDom->createElement( 'div' );
$oAttribute = $oElement->setAttribute( 'id', $strId );
$oElement->setIdAttribute( 'id', false ); // tried also without this
$oElement->appendChild( $oAttribute );
// $oAttribute = $oElement->getAttributeNode( 'id' );
$b = $oAttribute->isId();
if( $b ) {
echo "true";
} else {
echo "false"; // says false
}
$oElement = $m_oDom->getElementById( $strId );
if( $oElement ) {
echo "element";
} else {
echo "false"; // says false
}
?>