如何仅使用 class 名称获取 div 内的 <a href= 值?
How to get <a href= value inside div with class name only?
我正在尝试获取名称为 class 的 div 中 href 的值(class="visible-xs")。我试过这段代码,它得到了 div 之外的所有 href,我不想要。
$dom = new DOMDocument;
$dom->loadHTML($code2);
foreach ($dom->getElementsByTagName('a') as $node)
{
echo $node->getAttribute("href")."\n";
}
然后我尝试了以下操作,但它给了我错误(致命错误:调用未定义的方法 DOMDocument::getElementsByClassName() in..):
$dom = new DOMDocument;
$dom->loadHTML($code2);
foreach ($dom->getElementsByClassName('visible-xs') as $bigDiv) {
echo $bigDiv->getAttribute("href")."\n";
}
谁能帮我解决上面的错误,只获取 div 中 href 的值,名称为 class visible-xs ?提前致谢。
示例数据:
<tr class="ng-scope" ng-repeat="item in itemContent">
<td class="ng-binding" style="word-wrap: break-word;">test/folder/1.mp4
<div class="visible-xs" style="padding-top: 10px;">
<!-- ngIf: item.isViewable --> class="btn btn-default ng-scope" ng-click="$root.openView(item);">View</a><!-- end ngIf: item.isViewable -->
<a href="https://somesite.com/test/1.mp4" class="btn btn-default" ng-href="https://somesite.com/test/1.mp4" target="_blank">Download</a>
<a class="btn btn-default" href="javascript:void(0);" ng-click="item.upload()" target="_blank">Upload</a>
</div>
</td>
<!-- ngIf: hasViewables --><td class="text-right hidden-xs ng-scope" style="white-space: nowrap; width: 60px;" ng-if="hasViewables">
<!-- ngIf: item.isViewable -->class="btn btn-default ng-scope" ng-click="$root.openView(item);">View</a><!-- end ngIf: item.isViewable -->
</td><!-- end ngIf: hasViewables -->
<td class="text-right hidden-xs" style="white-space: nowrap; width: 250px;">
<a href="https://somesite.com/test/1.mp4" class="btn btn-default" ng-href="https://somesite.com/test/1.mp4" target="_blank">Download</a>
javascript:void(0);" ng-click="item.upload()" target="_blank">Upload</a>
</td>
</tr>
没有getElementsByClassName
函数。遍历您的 div,检查 class,如果匹配,拉出内部链接并输出您想要的 href
s(如果您想在第一次匹配后停止,则中断)。
$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($html);
libxml_clear_errors();
foreach ($dom->getElementsByTagName('div') as $div) {
if($div->getattribute('class') == 'visible-xs') {
foreach($div->getElementsByTagName('a') as $link) {
echo $link->getattribute('href');
}
}
}
示例 break
、https://eval.in/698488。
我正在尝试获取名称为 class 的 div 中 href 的值(class="visible-xs")。我试过这段代码,它得到了 div 之外的所有 href,我不想要。
$dom = new DOMDocument;
$dom->loadHTML($code2);
foreach ($dom->getElementsByTagName('a') as $node)
{
echo $node->getAttribute("href")."\n";
}
然后我尝试了以下操作,但它给了我错误(致命错误:调用未定义的方法 DOMDocument::getElementsByClassName() in..):
$dom = new DOMDocument;
$dom->loadHTML($code2);
foreach ($dom->getElementsByClassName('visible-xs') as $bigDiv) {
echo $bigDiv->getAttribute("href")."\n";
}
谁能帮我解决上面的错误,只获取 div 中 href 的值,名称为 class visible-xs ?提前致谢。
示例数据:
<tr class="ng-scope" ng-repeat="item in itemContent">
<td class="ng-binding" style="word-wrap: break-word;">test/folder/1.mp4
<div class="visible-xs" style="padding-top: 10px;">
<!-- ngIf: item.isViewable --> class="btn btn-default ng-scope" ng-click="$root.openView(item);">View</a><!-- end ngIf: item.isViewable -->
<a href="https://somesite.com/test/1.mp4" class="btn btn-default" ng-href="https://somesite.com/test/1.mp4" target="_blank">Download</a>
<a class="btn btn-default" href="javascript:void(0);" ng-click="item.upload()" target="_blank">Upload</a>
</div>
</td>
<!-- ngIf: hasViewables --><td class="text-right hidden-xs ng-scope" style="white-space: nowrap; width: 60px;" ng-if="hasViewables">
<!-- ngIf: item.isViewable -->class="btn btn-default ng-scope" ng-click="$root.openView(item);">View</a><!-- end ngIf: item.isViewable -->
</td><!-- end ngIf: hasViewables -->
<td class="text-right hidden-xs" style="white-space: nowrap; width: 250px;">
<a href="https://somesite.com/test/1.mp4" class="btn btn-default" ng-href="https://somesite.com/test/1.mp4" target="_blank">Download</a>
javascript:void(0);" ng-click="item.upload()" target="_blank">Upload</a>
</td>
</tr>
没有getElementsByClassName
函数。遍历您的 div,检查 class,如果匹配,拉出内部链接并输出您想要的 href
s(如果您想在第一次匹配后停止,则中断)。
$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($html);
libxml_clear_errors();
foreach ($dom->getElementsByTagName('div') as $div) {
if($div->getattribute('class') == 'visible-xs') {
foreach($div->getElementsByTagName('a') as $link) {
echo $link->getattribute('href');
}
}
}
示例 break
、https://eval.in/698488。