如何使用PHP或JavaScript在网页正文中显示description meta标记的内容?
我的站点以class.page.php构造函数/析构函数开始,该构造函数包含元信息、页面标题和页脚如何使用PHP或JavaScript在网页正文中显示description meta标记的内容?,javascript,php,Javascript,Php,我的站点以class.page.php构造函数/析构函数开始,该构造函数包含元信息、页面标题和页脚 <?php class page{ private $title; private $pageDescription; public function __construct($title, $pageDescription){ $this->title = $title; $this->pagedescription = $pa
<?php
class page{
private $title;
private $pageDescription;
public function __construct($title, $pageDescription){
$this->title = $title;
$this->pagedescription = $pageDescription;
<!DOCTYPE html>
<html lang="en">
<head>
<title>Page Title</title>
<meta name="description" content="My Page Description;">
// … etc …
function __destruct()
{
// … etc …
}
}
?>
我能够在如下所示的h1
标记之间显示页面标题,但我还没有弄清楚如何对元描述内容执行类似的操作
<h1 id="pagetitle"></h1>
<script>
document.getElementById('pagetitle').innerHTML = document.title;
</script>
document.getElementById('pagetitle')。innerHTML=document.title;
我更愿意为此找到一个PHP解决方案(以及页面标题)。但我也很乐意使用JavaScript来实现这一点。您可以使用meta[name=description]
选择器选择
元素:
const content=document.querySelector('meta[name=description')).getAttribute('content');
document.getElementById('pagetitle')。textContent=content代码>
<meta name="description" content="I want this also to show up between the h2 tags.">
<h2>How do I insert the page description here?</h2>
<h1 id="pagetitle"></h1>
<script>
document.getElementById('pagetitle').innerHTML = document.title;
</script>