我的动态网站有问题需要修复。该网站有一个定制的 CMS,但该 CMS 不会更改页面标题,只是添加内容和该内容的标题。内容的标题在网站页面中没有 ID 属性,但有一个 aria-current="page" 通用属性。所以我在想是否可以将这个 aria-current="page" 属性的内容作为页面标题。
我试图寻找 ID,但找不到。
// 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 代码