Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
导入div';使用PHP、jQuery或Ajax从静态HTML文件中删除内容?_Php_Jquery_Ajax_Html - Fatal编程技术网

导入div';使用PHP、jQuery或Ajax从静态HTML文件中删除内容?

导入div';使用PHP、jQuery或Ajax从静态HTML文件中删除内容?,php,jquery,ajax,html,Php,Jquery,Ajax,Html,你好朋友一个新手问题 问题是这样的: 我有一个静态HTML文件,我只想将该文件的一部分导入另一个页面。我该怎么做呢 示例代码: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Some title here</title> <link rel="styl

你好朋友一个新手问题

问题是这样的:

我有一个静态HTML文件,我只想将该文件的一部分导入另一个页面。我该怎么做呢

示例代码:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Some title here</title>

<link rel="stylesheet" type="text/css" href="styles.css" />

</head>

<body>

<div id="box-1">
    <div class="block">

    <!-- Some code here -->

    </div>
</div>

<div id="box-2">
    <div class="block">

    <!-- Some code here -->

    </div>
</div>


<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script src="script.js"></script>
</body>
</html>

这里有一些标题
现在我想要的是读取这个HTML文件并导入这一点:

<div id="box-1">
    <div class="block">

    <!-- Some code here -->

    </div>
</div>

有办法吗

请帮忙

即使是PHP、jQuery、Ajax或任何其他解决方案也可以。请帮助我。

您可以使用jQuery只指定要加载的特定容器:

$('#targetdiv').load('static.html #box-1');

这里有一个不同于其他解决方案的解决方案:它从大量静态html文件/页面(如果您需要的话)一次性导入一部分内容。我使用它成功地将大约700页从纯html导入cms数据库

// get the pages you want to import
$pages = array('page1.html', 
    'page2.html',
    'page3.html'
);

foreach($pages as $p) {
    $url = 'http://yourDomain.com/' . $p;

    // load the webpage
$file = file_get_contents($url);

if($file) {

    list($before,$content) = explode('<body>',$file); // chop off beginning
    unset($before);
    list($content) = explode('<div id="box-2">',$content); // chop off end;

            $resultArray[] = trim($content);

            // or do it this way to keep the filename associated with the content
            // $resultArray[$p] = $content;

}//if file

} //endforeach;

// $resultArray holds your stripped content
// do something with $resultArray;
//获取要导入的页面
$pages=array('page1.html',
“page2.html”,
“page3.html”
);
foreach($p页){
$url='1http://yourDomain.com/“.$p;
//加载网页
$file=文件获取内容($url);
如果($file){
list($before,$content)=分解(“”,$file);//切掉开头
未结算(以前为美元);
列表($content)=分解(“”,$content);//切掉结尾;
$resultArray[]=trim($content);
//或者这样做以保持文件名与内容关联
//$resultArray[$p]=$content;
}//如果文件
}//endforeach;
//$resultArray保存您的剥离内容
//使用$resultArray做一些事情;

这绝对是正确的选择。谢谢佩卡。你能帮忙真是太好了,真是太好了。但那不是我想要的。我更喜欢Pekka的jQuery解决方案。无论如何谢谢你的发帖。也许它会对其他正在寻找类似解决方案的人有用。