如何使用自定义元素使 href 动态化

发布于 2025-01-14 16:17:19 字数 364 浏览 2 评论 0原文

我想用 navs (本地网站)制作一个标题,并且相同的标题将在所有页面中重复。 因此,我没有复制粘贴它,而是在 js 中创建了一个自定义元素。 问题是主页中的 href 与其他页面不同。

这就是它在主页中的样子

<a href="pages/somepage.html">

这也是在同一文件夹中的其他页面中的样子

<a href="somepage.html">

使用自定义元素将使它们都具有相同的 href。

有没有解决方案,或者我错过了一些非常明显的东西? 我可以将它们全部放在同一个文件夹中,但添加的页面越多,情况就会变得混乱。

I wanted to make a header with navs (local website) , and the same header will be repeated in all pages.
So instead of copy pasting it, i made a custom element in js.
The problem is that the hrefs in the main page will be different than the other pages.

This is how it should be in the main page

<a href="pages/somepage.html">

And this is how it should be in other pages who are in the same folder

<a href="somepage.html">

Using a custom element will make them all have the same href.

Is there a solution to this or am i missing something really obvious ?
I could just have them all in the same folder but that will get messy the more pages i add.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾城花音 2025-01-21 16:17:19

通过在前面添加 / 来使您的 href 绝对化。

例如

<a href="/pages/somepage.html">

Make your href absolute by putting a / at the front.

eg

<a href="/pages/somepage.html">
落花随流水 2025-01-21 16:17:19

也许您创建以下链接:

<a href="/pages/somepage.html">

如果您将 / 添加到前面的链接浏览器搜索文件夹。

Maybe you make links following:

<a href="/pages/somepage.html">

if you add / to front link browser search the folder.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文