假设我有以下HTML结构:
<div>
<div class="content">
<p>This is dummy text</p>
<p><img src="a.jpg"></p>
<p>This is dummy text</p>
<p><img src="b.jpg"></p>
</div>
</div>
我想从.content
div获取所有图像src。我试过了:
<?php
// a new dom object
$dom = new domDocument;
// load the html into the object
$dom->loadHTML("example.com/article/2345");
// discard white space
$dom->preserveWhiteSpace = false;
//get element by class
$finder = new DomXPath($dom);
$classname = 'content';
$content = $finder->query("//*[contains(@class, '$classname')]");
foreach($content as $item){
echo $item->nodevalue;
}
但是,当我循环$content
时,我无法得到任何东西。请帮助。
答案 0 :(得分:4)
更改您的XPath查询,如下所示:
var purl = "http://www.othersite.com";
$.getJSON('http://whateverorigin.org/get?url=' +
encodeURIComponent(purl) + '&callback=?',
function (data) {
$('#viewer').html(data.contents);
});