【发布时间】:2022-12-08 04:33:00
【问题描述】:
页面的 html 标签没有 lang=en 属性,我试图将它添加到 html 标签,下面是我做的代码,但它替换了整个 html 内容,而不是仅仅将 lang=en 添加到 html。
window.addEventListener('load', function () {
alert("It's loaded!")
const cond = document.getElementsByTagName('html')[0]|| false;
console.log(cond)
if (cond) {
$('html').each(function() {
$(this).replaceWith($('<html lang="en">'));
});
}});
我也尝试了下面的代码,但它也不起作用,基本上它获取 html 内容并附加新的 html 标签和内容。
const htmlContent = $( "html" ).html();
if (cond) {
$('html').replaceWith('<html lang="en">' + htmlContent + '</html>');
}
【问题讨论】:
标签: javascript html