使用JS在HTML上创建自动标题

huangapple go评论65阅读模式
英文:

Make auto titles on html using JS

问题

以下是翻译好的部分:

  1. 检查是否已存在带有"id"为"titulo"的"h1"标签;如果不存在,则获取文档名称,删除扩展名,将"_"替换为" ",并将其添加到<h1 id="titulo">中。如果已存在,则不执行任何操作。
  2. <h1 id="titulo">的内部文本写入<title>标签中。

我得到了这个结果

控制台显示了如下信息:链接

英文:

The code below makes the first step: add an image with a link to the home-page and an h1 with the name of the document, if there isn't an h1 already.

I used an html that references a JavaScript file in the &lt;head&gt; and has a &lt;h1&gt; tag for the title (this is one of the expectations that I want to make this because of the special characters).

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

document.write(&#39;&lt;a href=&quot;rpg.html&quot;&gt;&lt;img src=&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/c/c5/U%2B2190.svg/25px-U%2B2190.svg.png&quot;&gt;&lt;/a&gt;&lt;br&gt;&#39;);

if (document.getElementById(titulo) != null) /*se existir um n&#227;o titulo... */ {
  var nome_do_documento = window.location.href.substring(window.location.href.lastIndexOf(&#39;/&#39;) + 1); /*colete o nome do documento */
  var titulo = nome_do_documento.replace(&#39;_&#39;, &#39; &#39;); /*troque _ por um espa&#231;o */
  var titulo_pronto = titulo.replace(&#39;.html&#39;, &#39;&#39;); /*apague a exten&#231;&#227;o do nome */
  document.write(&#39;&lt;h1 id=&quot;titulo&quot;&gt;&#39; + titulo_pronto + &#39;&lt;/h1&gt;&#39;);
}; /*e o nome no topo da pagina */

document.title = &quot;PVP - &quot; + document.getElementById(titulo).innerText; /*coloque o nome do topo da gina como titulo do html */

<!-- language: lang-html -->

&lt;h1 id=&quot;titulo&quot;&gt;kau&#227;&lt;/h1&gt;

<!-- end snippet -->

I expected these steps:

  1. check if there's already a h1 with the id of "titulo"; if not then get the document name, remove the extension name and replace "_" with " " and add this to the &lt;h1 id=&quot;titulo&quot;&gt;. If yes, do nothing.
  2. write the inner-text of &lt;h1 id=&quot;titulo&quot;&gt; in the &lt;title&gt; tag.

I got this result:

<a href="https://i.stack.imgur.com/aeZTf.png"><img src="https://i.stack.imgur.com/aeZTf.png"></a>

The console says this:

<a href="https://i.stack.imgur.com/ck7sT.png"><img src="https://i.stack.imgur.com/ck7sT.png"></a>

答案1

得分: 0

当查找一个元素的ID时,请确保将其作为一个字符串发送:

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>');

/* 如果存在一个名为 "titulo" 的元素... */
if (document.getElementById("titulo") != null )
{
  var nome_do_documento = window.location.href.substring(window.location.href.lastIndexOf('/')+1); 

  /* 收集文档名称 */
  var titulo = nome_do_documento.replace('_',' ');

  /* 用空格替换下划线 */
  var titulo_pronto = titulo.replace('.html','');

  /* 删除名称的扩展 */
  document.write('<h1 id="titulo">'+ titulo_pronto +'</h1>');
};

/* 将页面顶部的名称设置为HTML标题 */
document.title = "PVP - " + document.getElementById("titulo").innerText;

在你的示例中,引号缺失,这会被视为一个名为 titulo 的变量(第一次调用 getElementById() 时很可能是 undefined,然后在第二次调用时被设置为 nome_do_documento.replace('_',' '))。

英文:

When looking for an element by ID, be sure to send it as a string:

document.write(&#39;&lt;a href=&quot;rpg.html&quot;&gt;&lt;img src=&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/c/c5/U%2B2190.svg/25px-U%2B2190.svg.png&quot;&gt;&lt;/a&gt;&lt;br&gt;&#39;);

/*se existir um n&#227;o titulo... */
if (document.getElementById(&quot;titulo&quot;) != null )
{
  var nome_do_documento = window.location.href.substring(window.location.href.lastIndexOf(&#39;/&#39;)+1); 

  /*colete o nome do documento */
  var titulo = nome_do_documento.replace(&#39;_&#39;,&#39; &#39;);

  /*troque _ por um espa&#231;o */
  var titulo_pronto = titulo.replace(&#39;.html&#39;,&#39;&#39;);

  /*apague a exten&#231;&#227;o do nome */
  document.write(&#39;&lt;h1 id=&quot;titulo&quot;&gt;&#39;+ titulo_pronto +&#39;&lt;/h1&gt;&#39;);
};

/*e o nome no topo da pagina */
document.title = &quot;PVP - &quot; + document.getElementById(&quot;titulo&quot;).innerText;

/*coloque o nome do topo da gina como titulo do html */

In your example the quotes are missing, which is treated as a variable named titulo (likely to be undefined the first time getElementById() is called, then set to nome_do_documento.replace(&#39;_&#39;,&#39; &#39;) the second time).

答案2

得分: 0

在你的代码中,if语句的条件有错误。将"!="更改为"==",用引号包围getElementById中的id字符串,错误将被解决。

如果你愿意,这是我对你的代码的快速重构。

(() => {
    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>');

    let headline = document.querySelector('#titulo');
    let title = '';

    if (!headline) {
        const url = window.location.pathname;
        const fileName = url.substring(url.lastIndexOf('/') + 1);
        
        title = fileName.replace(/\.\w+$/gi, '').replace('_', ' ');

        headline = document.createElement('h1');
        headline.textContent = title;
        document.body.appendChild(headline);
    } else {
        title = headline.textContent;
    }

    document.title = 'PVP - ' + title;
})();

希望这对你有帮助。

英文:

In your code you have wrong condition in if statement. Change != to ==, surround id string inside getElementById with quotes and the error will be solved.

If you want, this is my quick refactor of your code.

(() =&gt; {
    document.write(&#39;&lt;a href=&quot;rpg.html&quot;&gt;&lt;img src=&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/c/c5/U%2B2190.svg/25px-U%2B2190.svg.png&quot;&gt;&lt;/a&gt;&lt;br&gt;&#39;);

    let headline = document.querySelector(&#39;#titulo&#39;);
    let title = &#39;&#39;;

    if (!headline) {

        const url = window.location.pathname;
        const fileName = url.substring(url.lastIndexOf(&#39;/&#39;) + 1);
        
        title = fileName.replace(/\.\w+$/gi, &#39;&#39;).replace(&#39;_&#39;, &#39; &#39;);

        headline = document.createElement(&#39;h1&#39;);
        headline.textContent = title;
        document.body.appendChild(headline);

    } else
        title = headline.textContent;

    document.title = &#39;PVP - &#39; + title;
})();

huangapple
  • 本文由 发表于 2023年2月27日 06:52:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75575478.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定