【发布时间】:2016-12-16 01:38:33
【问题描述】:
我试图在类名 (class="visible-xs") 的 div 中获取 href 的值。我尝试了这段代码,它也得到了 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";
}
任何人都可以帮我解决上述错误,并且只获取类名称为 visible-xs 的 div 内的 href 值吗?提前致谢。
样本数据:
<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>
【问题讨论】:
-
感谢您的回复。您能给我展示一个如何使用 xpath 来实现此目的的示例吗?
-
我尝试了以下但没有数据:$doc = new DOMDocument(); @$doc->loadHTML($code2); $xpath = new DomXPath($doc); foreach ($xpath->query('//div[class="visible-xs"]//a[@href]') as $link) { echo $link->getAttribute('href'); }
-
div类名是visible-xs,div里面的somesite.com/test/1.mp4 一次而不是多次
-
我在第一篇文章中更新了示例数据。不幸的是,数据是这样呈现的。数据来自远程网站,我无法控制它如何呈现数据!我只想获取此行的 href 值 somesite.com/test/1.mp4" class="btn btn-default" ng-href="somesite.com/test/1.mp4" target="_blank">下载 javascript:void (0);" ng-click="item.upload()" target="_blank">上传