我整晚都在考虑解决方案,但我无法弄清楚。从URL数据中获取内容按类或ID使用JS
我有下载网址内容PHP代码:
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$data = get_data($url);
,我想从div的获取内容或任何从这个类名或ID,所以我发这个变量JS是这样的:
<script>
var data = <?php echo json_encode($data); ?>;
alert(data);
//alert shows all code of downloaded URL
//$(".content").html(data);
//this line show whole page but I want only specific element/class
</script>
我该如何做到这一点?假设我想从JavaScript中的“data”变量中获得class =“productMainTitle”的H1标签内容?
任何提示将不胜感激!
这就是我正在寻找的!非常感谢,它工作完美:)我猜json_encode是需要的,因为没有这个代码再次显示整个页面^ – 2014-10-04 11:29:29