如何使用自定义元素使 href 动态化
我想用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过在前面添加 / 来使您的 href 绝对化。
例如
Make your href absolute by putting a / at the front.
eg
也许您创建以下链接:
如果您将 / 添加到前面的链接浏览器搜索文件夹。
Maybe you make links following:
if you add / to front link browser search the folder.