gpt4 book ai didi

javascript - 如何在 HTML 链接中使用 JavaScript 变量

转载 作者:行者123 更新时间:2023-11-28 16:13:03 25 4
gpt4 key购买 nike

我正在开发的网站有一个 <base>标签指向与网站所具有的 URL 不同的 URL。我想做的是绕过 <base>使用下面的 trueURL 标记来查找网页的网址,因为我需要它来构造一些内部 anchor ,因为我需要网站的实际网址,以便内部 anchor 正常工作。

我遇到的问题是我不知道应该如何使用存储在 trueURL 变量中的 url。是否可以使用它,然后向网址添加额外的扩展名以使其指向我的 anchor ?下面是我希望能够做的一个粗略示例

var trueURL = window.location.href;

<html>

<ol>
<li>
<a href= [trueURL] + "#link1">Link1</a>
</li>

<li>
<a href= [trueURL] + "#link2">Link2</a>
</li>

<li>
<a href= [trueURL] + "#link3">Link2</a>
</li>
</ol>

</html>

因此最后我想要一个看起来像 trueURL#link3 的链接。

提前致谢,

:D:D

最佳答案

我正在假设您的真实案例比您的示例更复杂。如果不是,请查看其他答案,它们可能更适合您需要执行的操作。

这将做什么......

  1. 窗口加载事件上运行一个函数,该函数将...
  2. 查找 DOM 中属于 A 标记的每个元素,并且...
  3. 循环遍历那些找到的 A 标签,并检查该元素是否具有 enhance 类,如果有的话...
  4. #处拆分href以获取当前a元素的hash值,我们将这样做。 ..
  5. 将其与以下内容组合:

    '//' + location.host + location.pathname;

    向我们提供当前页面的 URL,不带 哈希 或查询字符串。如果您想要查询字符串(URL 中 ? 后面的内容),请添加 location.search:

    '//' + location.host + location.pathname + location.search;

请注意,// 部分是与协议(protocol)相关的,因此它将使用 base href 的协议(protocol),例如 httphttps。不过,您可能需要指定这一点。

标记

注意 class 属性,我们将使用它来识别要操作的 a 标记。

<ol>
<li>
<a href="#link1">Link 1</a> -
No enhance class, should be subject to <strong>BASE HREF</strong>:
<strong class="p">&raquo; http://example.com#link1</strong>
</li>
<li>
<a href="#link2" class="enhance">Link 2</a> -
Has enhance class, should be:
<strong class="p">&raquo; http://fiddle.jshell.net/_display/#link2</strong>
</li>
<li>
<a href="#link3" class="enhance">Link 3</a> -
Has enhance class, should be:
<strong class="p">&raquo; http://fiddle.jshell.net/_display/#link3</strong>
</li>
<li>
<a href="#link3" class="enhance">Link 4</a> -
Has enhance class, should be:
<strong class="p">&raquo; http://fiddle.jshell.net/_display/#link4</strong>
</li>
</ol>​

Javascript

//--
// window.onload is not preferred, window.addEventListener
// should be used. Except that only IE9 supports it, and
// IE8 and lower do not support it, and uses window.attachEvent
// instead, which you can test for.
//
// I'm using window.onload here simply as a shortcut method and
// for clarity. This can also go in a script tag at the bottom
// of the body tag, in which case you can remove window.onload
// and use a self-calling anonymous function like:
//
// (function updateLinks(){... all the same as below ...})();
//
// This will ensure you are out of the global variable scope.
//--
window.onload = function updateLinks() {
//--
// Modern browsers can use document.getElementsByClassName
// or you can use a shiv script to add it to browsers that
// don't. I'll do it the "manual" way here, and this works
// cross-browser. The downside is that it will interate
// through every A tag on the page, looking for the "small"
// few that have the el.className we're looking for here.
//--
var els = document.getElementsByTagName("a"),
l = els.length,
trueURL = '//' + location.host + pathname,
i, el, hash;

for (i = 0; i < l; i++) {
el = els[i];

if (el.className.indexOf("enhance") != -1) {
hash = el.href.split('#');
el.href = trueURL + "#" + hash[1];
}
}
};

http://jsfiddle.net/userdude/unnH8/

将鼠标悬停在链接上可查看当前设置。一如既往,在多个浏览器中使用真实标记进行彻底测试

关于javascript - 如何在 HTML 链接中使用 JavaScript 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12314799/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com