<legend id='81ujk'><style id='81ujk'><dir id='81ujk'><q id='81ujk'></q></dir></style></legend>
      <bdo id='81ujk'></bdo><ul id='81ujk'></ul>

      1. <i id='81ujk'><tr id='81ujk'><dt id='81ujk'><q id='81ujk'><span id='81ujk'><b id='81ujk'><form id='81ujk'><ins id='81ujk'></ins><ul id='81ujk'></ul><sub id='81ujk'></sub></form><legend id='81ujk'></legend><bdo id='81ujk'><pre id='81ujk'><center id='81ujk'></center></pre></bdo></b><th id='81ujk'></th></span></q></dt></tr></i><div id='81ujk'><tfoot id='81ujk'></tfoot><dl id='81ujk'><fieldset id='81ujk'></fieldset></dl></div>
      2. <tfoot id='81ujk'></tfoot>

      3. <small id='81ujk'></small><noframes id='81ujk'>

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

        How to use JavaScript variable in an HTML link(如何在 HTML 链接中使用 JavaScript 变量)
        <tfoot id='dNji1'></tfoot>
          • <bdo id='dNji1'></bdo><ul id='dNji1'></ul>

              <legend id='dNji1'><style id='dNji1'><dir id='dNji1'><q id='dNji1'></q></dir></style></legend>
                <i id='dNji1'><tr id='dNji1'><dt id='dNji1'><q id='dNji1'><span id='dNji1'><b id='dNji1'><form id='dNji1'><ins id='dNji1'></ins><ul id='dNji1'></ul><sub id='dNji1'></sub></form><legend id='dNji1'></legend><bdo id='dNji1'><pre id='dNji1'><center id='dNji1'></center></pre></bdo></b><th id='dNji1'></th></span></q></dt></tr></i><div id='dNji1'><tfoot id='dNji1'></tfoot><dl id='dNji1'><fieldset id='dNji1'></fieldset></dl></div>

                • <small id='dNji1'></small><noframes id='dNji1'>

                    <tbody id='dNji1'></tbody>
                  本文介绍了如何在 HTML 链接中使用 JavaScript 变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

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

                  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. window 加载事件中运行一个函数,该函数将...
                  2. DOM 中找到每个 A 标签的元素,然后...
                  3. 遍历找到的A标签,检查元素是否有enhance类,如果有的话...
                  4. # 处拆分 href 以获得当前 a 元素的 href 值,我们将这样做...
                  5. 将其与以下内容结合:

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

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

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

                  注意,// 部分是协议相关的,所以它会使用 base href 的任何协议,例如 httphttps.但是,您可能需要指定.

                  标记

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

                    <li><a href="#link1">链接 1</a>-无增强类,应服从<strong>BASE HREF</strong>:<strong class="p">&raquo;http://example.com#link1</strong></li><li><a href="#link2" class="enhance">链接 2</a>-有增强类,应该是:<strong class="p">&raquo;http://fiddle.jshell.net/_display/#link2</strong></li><li><a href="#link3" class="enhance">链接 3</a>-有增强类,应该是:<strong class="p">&raquo;http://fiddle.jshell.net/_display/#link3</strong></li><li><a href="#link3" class="enhance">链接 4</a>-有增强类,应该是:<strong class="p">&raquo;http://fiddle.jshell.net/_display/#link4</strong></li></ol>

                  Javascript

                  //--//window.onload 不是首选,window.addEventListener//应该使用.除了只有 IE9 支持,而且//IE8及以下不支持,使用window.attachEvent//相反,您可以对其进行测试.////我在这里使用 window.onload 只是作为一种快捷方式,并且//为了清楚起见.这也可以放在底部的脚本标签中//body 标签,在这种情况下你可以删除 window.onload//并使用一个自调用匿名函数,例如:////(function updateLinks(){... 和下面一样...})();////这将确保您不在全局变量范围内.//--window.onload = 函数 updateLinks() {//--//现代浏览器可以使用 document.getElementsByClassName//或者您可以使用 shiv 脚本将其添加到浏览器//别.我将在这里以手动"方式进行操作,这很有效//跨浏览器.缺点是会交互//通过页面上的每个 A 标签,寻找小"//很少有我们在这里寻找的 el.className.//--var els = document.getElementsByTagName("a"),l = els.length,trueURL = '//' + location.host + 路径名,我,埃尔,哈希;对于 (i = 0; i < l; i++) {埃尔=埃尔斯[i];if (el.className.indexOf("enhance") != -1) {hash = el.href.split('#');el.href = trueURL + "#" + hash[1];}}};

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

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

                  The website that I am working on has a <base> tag point to a different URL than the one that the website has. What I would like to do is get around the <base> tag by using the trueURL bellow to find the url of the webpage, because i need it to construct some internal anchors, because i need the actual url of the website so the internal anchors work correctly.

                  The issue that im having is that i don't know how i should use the url that i store in my trueURL variable. Is it possible to use it and then add extra extensions to the url to get it to point to my anchors? Below is a rough example of what I would like to be able to do

                  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>
                  

                  Therefore in the end i would like to have a link that looks like trueURL#link3.

                  Thanks in advance,

                  :D :D

                  解决方案

                  I am working on the assumption that your real case is more complex than your example. If it isn't, then review the other answers, which may be more appropriate for what you need to do.

                  What this will do is...

                  1. Run on the window load event a function that will...
                  2. Find every element in the DOM that is an A tag, and...
                  3. Loop through those found A tags and check if the element has an enhance class, and if so...
                  4. Split the href at the # to get the current a element's hash value, which we will...
                  5. Combine it with the following:

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

                    Giving us the current page's URL without a hash or query string. If you want the query string (what's after the ? in a URL), add location.search:

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

                  Note, the // part is protocol relative, so it will use whatever the base href's protocol is, e.g., http or https. You may want to specify that, however.

                  Markup

                  Note the class attribute, which we will use to identify which a tags to manipulate.

                  <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/

                  Mouseover the links to see the current setting. As always, thoroughly test with real markup in multiple browsers.

                  这篇关于如何在 HTML 链接中使用 JavaScript 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  quot;Status Code:200 OK (from ServiceWorker)quot; in Chrome Network DevTools?(“状态码:200 OK(来自 ServiceWorker)在 Chrome 网络开发工具中?)
                  How to set a header for a HTTP GET request, and trigger file download?(如何为 HTTP GET 请求设置标头并触发文件下载?)
                  Adding custom HTTP headers using JavaScript(使用 JavaScript 添加自定义 HTTP 标头)
                  What is quot;X-Content-Type-Options=nosniffquot;?(什么是“X-Content-Type-Options=nosniff?)
                  SQL Query DocumentDB in Azure Functions by an integer not working(通过整数在 Azure Functions 中 SQL 查询 DocumentDB 不起作用)
                  Azure Functions [JavaScript / Node.js] - HTTP call, good practices(Azure Functions [JavaScript/Node.js] - HTTP 调用,良好实践)

                    • <bdo id='ODCbw'></bdo><ul id='ODCbw'></ul>
                      1. <tfoot id='ODCbw'></tfoot>
                          <tbody id='ODCbw'></tbody>
                          <legend id='ODCbw'><style id='ODCbw'><dir id='ODCbw'><q id='ODCbw'></q></dir></style></legend>
                          • <i id='ODCbw'><tr id='ODCbw'><dt id='ODCbw'><q id='ODCbw'><span id='ODCbw'><b id='ODCbw'><form id='ODCbw'><ins id='ODCbw'></ins><ul id='ODCbw'></ul><sub id='ODCbw'></sub></form><legend id='ODCbw'></legend><bdo id='ODCbw'><pre id='ODCbw'><center id='ODCbw'></center></pre></bdo></b><th id='ODCbw'></th></span></q></dt></tr></i><div id='ODCbw'><tfoot id='ODCbw'></tfoot><dl id='ODCbw'><fieldset id='ODCbw'></fieldset></dl></div>

                            <small id='ODCbw'></small><noframes id='ODCbw'>