使用 JS 在 html 上制作自动标题

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

代码使第一步:添加带有主页链接的图像和带有文档名称的 h1(如果还没有 h1)

我使用了一个引用 js 文件的 html 并且标题有一个 h1 标签(这是我想做的例外之一,因为特殊字符)

<!DOCTYPE html>
<html>
<head>
  <meta charset='utf-8'>
  <meta http-equiv='X-UA-Compatible' content='IE=edge'>
  <title>title</title>
  <meta name='viewport' content='width=device-width, initial-scale=1'>
  <link rel='stylesheet' type='text/css' media='screen' href='main.css'>
  <script src='main.js'></script>
</head>
<body>
  <h1 id="titulo">kauã</h1>
  
</body>
</html>

main.js:

document.write('<a href="rpg.html"><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/c/c5/U%2B2190.svg/25px-U%2B2190.svg.png"></a><br>');
if (document.getElementById(titulo) != null )/*se existir um não titulo... */
{var nome_do_documento = window.location.href.substring(window.location.href.lastIndexOf('/')+1); /*colete o nome do documento */
var titulo = nome_do_documento.replace('_',' ');/*troque _ por um espaço */
var titulo_pronto = titulo.replace('.html','');/*apague a extenção do nome */
document.write('<h1 id="titulo">'+ titulo_pronto +'</h1>');};/*e o nome no topo da pagina */
document.title = "PVP - " + document.getElementById(titulo).innerText;/*coloque o nome do topo da gina como titulo do html */

我期待这个步骤:

  1. 检查是否有id为“titulo”的h1
  • 如果不是 -> 获取文档名称,删除扩展名并将“_”替换为“”并将其添加到
    <h1 id="titulo">
  • 如果是->什么都不做
  1. <h1 id="titulo">
    标签里写
    <tittle>
    的内文

我明白了:

result

控制台是这样说的:

console img

javascript html dom
© www.soinside.com 2019 - 2024. All rights reserved.