繁体   English   中英

如何在锚标记内使用 Javascript 变量作为 href ?

[英]How can I use a Javascript variable as href inside an anchor tag?

我正在为具有类似名称的页面设置分页系统。 我能够建立一个系统,该系统采用我的原始页面的 url 并创建“下一个”和“上一个”链接。 不过这些链接都是JS变量的形式,不知道要不要插入锚标签里面。

我目前的代码是:

 function break_address(url_add) { var page = url_add.split("/").slice(-1); var num = url_add.split('page-')[1]; [1]; //next var numnext = parseInt(num)+1; var numprev = parseInt(num)-1; var nextpage = "https://MYCOMIC.neocities.org/page-" + numnext + ".html"; var prevpage = "https://MYCOMIC.neocities.org/page-" + numprev + ".html"; //this return is only so that I can make sure the new links were created correctly return [num, numnext,numprev,nextpage,prevpage] document.getElementById("next").setAttribute("href",nextpage); } //in the real code i am adding the current url with window.location.href instead var url_add = "https://MYCOMIC.neocities.org/page-2.html" console.log("Original address: "+url_add) console.log(break_address(url_add))
 <!--the links are set to # right now because I don't know how to set them up. I originally tried setting them as <a id="next">link</a>, but this resulted in an un-clickable link.--> <a href="#"> <img src="previous.png" alt="PREVIOUS PAGE" style="height: 75px; width: 75px; padding-left 50px, padding-right: 50px;"></a> <a href="#"> <img src="next.png" alt="NEXT PAGE" style="height: 75px; width: 75px; padding-left 50px, padding-right: 50px;"></a>

在这里的另一个类似问题中,有人建议添加一些类似的内容

document.getElementById("next").href = nextpage;

在脚本中,然后通过执行调用它

<a id="nextpage">link</a>

但是,当我尝试执行此操作时,无法单击该链接。

我尝试将 href 添加回锚标记<a href="#" id="nextpage">link</a> ) 但随后链接转到我已经在的同一页面。

我究竟做错了什么? (注意:新生成的 url 链接的文件都存在。)

我知道我可以手动链接下一页和上一页。 我不想要这个,我想要可以自动链接到由 Javascript 生成的 url 的代码。 这可能吗? 我怎么能做到?

这是使用anchor标记的正确方法:

<a href="yourPagePath/page-1.html">link</a>

在这里的另一个类似问题中,有人建议添加以下内容: document.getElementById("next").href = nextpage;

从您提供的 html 代码来看,您似乎从未在a标签上指定 id,因此没有检索到任何元素。

但是,另一种解决方案是将 html 文件的路径嵌入到a标签的 href 中,如下所示:

<a href="path_to_html_file/page-1.html">link</a>

感谢所有试图提供帮助的人,我最终在 IRL 认识的人的帮助下解决了这个问题。 我的最终代码如下所示:

 //takes my current page's url which is in the form path/page-#.html and splits off the #.html function get_nextpage(url_add) { var page = url_add.split("/").slice(-1); var num = url_add.split('page-')[1]; [1]; //adds 1 to the number within the #.html, makes a new link which is "nextpage" var numnext = parseInt(num)+1; var nextpage = "https://MYCOMIC.neocities.org/page-" + numnext + ".html"; return nextpage; } //same but for previous page function get_prevpage(url_add) { var page = url_add.split("/").slice(-1); var num = url_add.split('page-')[1]; [1]; var numprev = parseInt(num)-1; var prevpage = "https://MYCOMIC.neocities.org/page-" + numprev + ".html"; return prevpage; } //function which uses the new url whenever my links are clicked window.onload = function(e) { var url_add = window.location.href; var nextpage = get_nextpage(url_add); var prevpage = get_prevpage(url_add); document.getElementById("next").onclick = function() { this.href = nextpage; console.log(this.href); }; document.getElementById("prev").onclick = function() { this.href = prevpage; console.log(this.href); }; }();
 <a class="button" id="prev" href="#"> <img src="previous.png" alt="PREVIOUS PAGE" style="height: 75px; width: 75px; padding-left 50px, padding-right: 50px;"></a> <a class="button" id="next" href="#" > <img src="next.png" alt="NEXT PAGE" style="height: 75px; width: 75px; padding-left 50px, padding-right: 50px;"></a>

它可能比理想情况复杂一点,但它有效!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM