Javascript 使用JS按类或ID从URL数据获取内容
我整晚都在想解决办法,但我想不出来 我有下载URL内容的PHP代码:Javascript 使用JS按类或ID从URL数据获取内容,javascript,php,jquery,css,curl,Javascript,Php,Jquery,Css,Curl,我整晚都在想解决办法,但我想不出来 我有下载URL内容的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);
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);
我想通过类名或ID从div或其他任何地方获取内容,所以我将此变量发送给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>
var数据=;
警报(数据);
//警报显示已下载URL的所有代码
//$(“.content”).html(数据);
//这一行显示整个页面,但我只需要特定的元素/类
我怎样才能做到这一点?假设我想从Javascript中的“data”变量中获取带有class=“productMainTitle”的H1标记的内容
任何提示都将不胜感激 使用DomParser
var parser = new DomParser();
var doc = parser.parseFromString(data, "text/xml");
然后像往常一样从文档中获取所需的元素
doc.getElementById("id")
并将其html分配给您的div
编辑:
使用DomParser
var parser = new DomParser();
var doc = parser.parseFromString(data, "text/xml");
然后像往常一样从文档中获取所需的元素
doc.getElementById("id")
并将其html分配给您的div
编辑:
使用DomParser
var parser = new DomParser();
var doc = parser.parseFromString(data, "text/xml");
然后像往常一样从文档中获取所需的元素
doc.getElementById("id")
并将其html分配给您的div
编辑:
使用DomParser
var parser = new DomParser();
var doc = parser.parseFromString(data, "text/xml");
然后像往常一样从文档中获取所需的元素
doc.getElementById("id")
并将其html分配给您的div
编辑:
这完全取决于你想做什么 1。仅发送HTML客户端的相关片段
<script>
var $data = $("<?php echo $data; ?>");
$data.find("h1.productMainTitle").appendTo(".content");
</script>
见@artm的答案
2。发送所有HTML客户端并在那里进行排序
<script>
var $data = $("<?php echo $data; ?>");
$data.find("h1.productMainTitle").appendTo(".content");
</script>
var$data=$(“”);
$data.find(“h1.productMainTitle”).appendTo(.content”);
假设不需要json\u encode()
。如果我错了,请重新插入
如果您只需要“h1.productMainTitle”片段,则效率低于(1),但如果您需要更多片段,则这是一种可行的方法。完全取决于您想要做什么 1。仅发送HTML客户端的相关片段
<script>
var $data = $("<?php echo $data; ?>");
$data.find("h1.productMainTitle").appendTo(".content");
</script>
见@artm的答案
2。发送所有HTML客户端并在那里进行排序
<script>
var $data = $("<?php echo $data; ?>");
$data.find("h1.productMainTitle").appendTo(".content");
</script>
var$data=$(“”);
$data.find(“h1.productMainTitle”).appendTo(.content”);
假设不需要json\u encode()
。如果我错了,请重新插入
如果您只需要“h1.productMainTitle”片段,则效率低于(1),但如果您需要更多片段,则这是一种可行的方法。完全取决于您想要做什么 1。仅发送HTML客户端的相关片段
<script>
var $data = $("<?php echo $data; ?>");
$data.find("h1.productMainTitle").appendTo(".content");
</script>
见@artm的答案
2。发送所有HTML客户端并在那里进行排序
<script>
var $data = $("<?php echo $data; ?>");
$data.find("h1.productMainTitle").appendTo(".content");
</script>
var$data=$(“”);
$data.find(“h1.productMainTitle”).appendTo(.content”);
假设不需要json\u encode()
。如果我错了,请重新插入
如果您只需要“h1.productMainTitle”片段,则效率低于(1),但如果您需要更多片段,则这是一种可行的方法。完全取决于您想要做什么 1。仅发送HTML客户端的相关片段
<script>
var $data = $("<?php echo $data; ?>");
$data.find("h1.productMainTitle").appendTo(".content");
</script>
见@artm的答案
2。发送所有HTML客户端并在那里进行排序
<script>
var $data = $("<?php echo $data; ?>");
$data.find("h1.productMainTitle").appendTo(".content");
</script>
var$data=$(“”);
$data.find(“h1.productMainTitle”).appendTo(.content”);
假设不需要json\u encode()
。如果我错了,请重新插入
如果您只需要“h1.productMainTitle”片段,则效率低于(1),但如果您需要更多片段,则这是一种可行的方法。使用
按URL打开子页面,并在必要时隐藏它们
现在我们可以不用URL了:创建空框架并强制在其中写入一些html
按步骤(按按钮)尝试以下脚本以查看发生了什么:
<body>
<button onclick="createFrame();">createFrame</button>
<button onclick="getElement();">getElement</button>
<button onclick="closeFrame();">closeFrame</button>
<button onclick="showExtractedElement();">showExtractedElement</button>
<script>
var frame
, frameDoc
, elem
, htmlData = '<html><body><h1 class="myClass">Hello</h1></body></html>'
/*somehow we got this string containing html data*/
function createFrame()
{
frame = document.createElement( "iframe" );
frame.setAttribute( 'hidden', '' ); // on debug remove this line to see the frame
document.body.appendChild( frame );
frame.contentDocument.write(htmlData)
}
function getElement( )
{
elem = frame.contentDocument.querySelector( '.myClass' )
}
function closeFrame()
{
document.body.removeChild( frame );
}
// the extracted element remains
function showExtractedElement()
{
console.log( elem )
}
// call this method from console or script to do all the magic
function doEverything()
{
createFrame()
getElementFromAnotherPage()
closeFrame()
showExtractedElement()
}
</script>
</body>
createFrame
getElement
封闭框架
显示提取元素
可变框架
,frameDoc
,elem
,htmlData='Hello'
/*不知怎的,我们得到了这个包含html数据的字符串*/
函数createFrame()
{
frame=document.createElement(“iframe”);
frame.setAttribute('hidden','');//在调试时删除此行以查看帧
document.body.appendChild(框架);
frame.contentDocument.write(htmlData)
}
函数getElement()
{
elem=frame.contentDocument.querySelector('.myClass')
}
函数closeFrame()
{
document.body.removeChild(框架);
}
//提取的元素仍然存在
函数showExtractedElement()
{
控制台日志(elem)
}
//从控制台或脚本调用此方法来完成所有的工作
函数doEverything()
{
createFrame()
getElementFromAnotherPage()
closeFrame()
showExtractedElement()
}
使用
按URL打开子页面,必要时将其隐藏
现在我们可以不用URL了:创建空框架并强制在其中写入一些html
按步骤(按按钮)尝试以下脚本以查看发生了什么:
<body>
<button onclick="createFrame();">createFrame</button>
<button onclick="getElement();">getElement</button>
<button onclick="closeFrame();">closeFrame</button>
<button onclick="showExtractedElement();">showExtractedElement</button>
<script>
var frame
, frameDoc
, elem
, htmlData = '<html><body><h1 class="myClass">Hello</h1></body></html>'
/*somehow we got this string containing html data*/
function createFrame()
{
frame = document.createElement( "iframe" );
frame.setAttribute( 'hidden', '' ); // on debug remove this line to see the frame
document.body.appendChild( frame );
frame.contentDocument.write(htmlData)
}
function getElement( )
{
elem = frame.contentDocument.querySelector( '.myClass' )
}
function closeFrame()
{
document.body.removeChild( frame );
}
// the extracted element remains
function showExtractedElement()
{
console.log( elem )
}
// call this method from console or script to do all the magic
function doEverything()
{
createFrame()
getElementFromAnotherPage()
closeFrame()
showExtractedElement()
}
</script>
</body>
createFrame
getElement
封闭框架
显示提取元素
可变框架
,frameDoc
,elem
,htmlData='Hello'
/*不知怎的,我们得到了这个包含html数据的字符串*/
函数createFrame()
{
frame=document.createElement(“iframe”);
frame.setAttribute('hidden','');//在调试时删除此行以查看帧
document.body.appendChild(框架);
frame.contentDocument.write(htmlData)
}
函数getElement()
{
elem=frame.contentDocument.querySelector('.myClass')
}
函数closeFrame()
{
document.body.removeChild(框架);
}
//提取的元素仍然存在
函数showExtractedElement()
{
控制台日志(elem)
}
//从控制台或脚本调用此方法来完成所有的工作
函数doEverything()
{
createFrame()
getElementFromAnotherPage()
closeFrame()
嘘