简体   繁体   English

如何使用 PHP 或 JavaScript 在网页正文中显示描述元标记的内容?

[英]How can I use either PHP or JavaScript to display the contents of the description meta tag in the body text of a web page?

My site starts with a class.page.php constructor/destructor that contains the meta info, page title and footer.我的网站以 class.page.php 构造函数/析构函数开头,其中包含元信息、页面标题和页脚。

<?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 …
    }
}
?>

Each page begins with this PHP script to require the class.page.php and insert that page's title and description into the head.每个页面都以这个 PHP 脚本开始,需要 class.page.php 并将该页面的标题和描述插入到头部。

<?php
require_once("inc/class.page.php");
// Insert title, page description.
$page = new page('My Page Title','My Page Description');
?>

My goal is to be able to insert the description content from the meta tag…我的目标是能够从元标记插入描述内容......

<meta name="description" content="I want this also to show up between the h2 tags.">

…in between the h2 tags in the body text, ie: ...在正文中的h2标签之间,即:

<h2>How do I insert the page description here?</h2>

I'm able to get the page title to show between the h1 tags as shown below, but I haven't been able to figure out how to do something similar with the meta description content.我能够让页面标题显示在h1标签之间,如下所示,但我无法弄清楚如何对元描述内容执行类似的操作。

<h1 id="pagetitle"></h1>
<script>
    document.getElementById('pagetitle').innerHTML = document.title;
</script>

I would prefer to find a PHP solution for this (and for the page title, too, for that matter).我更愿意为此找到一个 PHP 解决方案(以及页面标题,就此而言)。 but I'd also be happy also to do this using JavaScript.但我也很乐意使用 JavaScript 来做到这一点。

You can select a <meta name="description"> element with a meta[name=description] selector:您可以使用meta[name=description]选择器选择<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."> <h1 id="pagetitle"></h1>

Also remember to only use innerHTML when deliberately inserting HTML markup - if you only want to insert text, use textContent instead.还记得在故意插入 HTML 标记时只使用innerHTML - 如果您只想插入文本,请改用textContent ( innerHTML is less appropriate, slower, and can allow for arbitrary code execution if the content being set isn't trustworthy) innerHTML不太合适,速度较慢,如果设置的内容innerHTML则可以允许任意代码执行)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 python 或 javascript 获取 web 页面的元描述标签 - How to get Meta Description tag of a web page using python or javascript 如何找到给定网址的meta标记中包含的页面描述(如果有描述)? - How can I find the page description contained in the meta tag of a given url (if it has a description)? 如何使用Javascript将另一个网页上的文本显示到另一个页面上? - How can I use Javascript to display text from another web page onto a different page? 如何使用php或js复制元描述的页面内容并限制为150个字符? - How can I copy page content for meta description and limit to 150 characters with php or js? 如何使用带有JavaScript标签的php? - how can I use php with javascript tag? 如何使用Perl访问JavaScript驱动的网页的内容? - How can I access the contents of a JavaScript driven web page with Perl? 将JavaScript变量存储在PHP变量中,以将其用于源页面中的动态元描述 - Storing the JavaScript variable in a PHP variable to use it for dynamic meta description in source page 如何使用Javascript和regex将RSS feed的描述标签中的多张图像解析到我的描述页面 - How can i parse the Multiple images in the description tag of my RSS feed to my description page using Javascript and regex 如何使用JavaScript将head标签中的内容移动到body标签? - How can I use JavaScript to move content in head tag to the body tag? 如何使用Perl从使用JavaScript动态生成的网页中获取文本? - How can I use Perl to grab text from a web page that is dynamically generated with JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM