亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 PHP 或 JavaScript 在網頁正文中顯示描述元標記的內容?

如何使用 PHP 或 JavaScript 在網頁正文中顯示描述元標記的內容?

PHP
翻翻過去那場雪 2024-01-19 20:59:07
我的網站以 class.page.php 構造函數/析構函數開始,其中包含元信息、頁面標題和頁腳。<?phpclass page{    private $title;    private $pageDescription;        public function __construct($title, $pageDescription){    $this->title = $title;    $this->pagedescription = $pageDescription;<!DOCTYPE html><html><head><title>Page Title</title><meta name="description" content="My Page Description;">//  … etc …    function __destruct()    {//  … etc …    }}?>每個頁面都以這個 PHP 腳本開始,要求 class.page.php 并將該頁面的標題和描述插入到頭部。<?phprequire_once("inc/class.page.php");// Insert title, page description.$page = new page('My Page Title','My Page Description');?>我的目標是能夠從元標記插入描述內容......<meta name="description" content="I want this also to show up between the h2 tags.">h2...在正文中的標簽之間,即:<h2>How do I insert the page description here?</h2>我能夠讓頁面標題顯示在標簽之間h1,如下所示,但我無法弄清楚如何對元描述內容執行類似的操作。<h1 id="pagetitle"></h1><script>    document.getElementById('pagetitle').innerHTML = document.title;</script>我更愿意為此找到一個 PHP 解決方案(以及頁面標題)。但我也很樂意使用 JavaScript 來完成此操作。
查看完整描述

1 回答

?
蕪湖不蕪

TA貢獻1796條經驗 獲得超7個贊

您可以使用選擇器選擇一個<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>

另請記住,僅在有意插入 HTML 標記時才使用innerHTML- 如果您只想插入文本,請textContent改為使用。(innerHTML不太合適,速度較慢,并且如果設置的內容不可信,則可能允許執行任意代碼)



查看完整回答
反對 回復 2024-01-19
  • 1 回答
  • 0 關注
  • 146 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號