根据 aria-current ="page" 内容更改页面标题

问题描述 投票:0回答:1

我的动态网站有问题需要修复。该网站有一个定制的 CMS,但该 CMS 不会更改页面标题,只是添加内容和该内容的标题。内容的标题在网站页面中没有 ID 属性,但有一个 aria-current="page" 通用属性。所以我在想是否可以将这个 aria-current="page" 属性的内容作为页面标题。 enter image description here

我试图寻找 ID,但找不到。

javascript html content-management-system
1个回答
0
投票
// changeTitle.js
// Get the content of aria-current="page" attribute
var currentPageElement = document.querySelector('[aria-current="page"]');

if (currentPageElement) {
    var currentPageContent = currentPageElement.textContent || currentPageElement.innerText;

    // Set the content as the title of the webpage
    document.title = currentPageContent;
} else {
    console.warn('No element with aria-current="page" found.');
}

这是 ChatGPT 代码

© www.soinside.com 2019 - 2024. All rights reserved.