繁体   English   中英

将js变量传递给href

[英]Pass a js variable to a href

我正在处理某些链接,但无法正常工作! 我会知道它如何与php一起工作,但我很讨厌js:P

因此,我将fancybox用于某些简单形式。 但是我需要将文档的当前URL传递到该页面,因此我们知道填写该表单的人来自哪里! 每个网址都像

example.com/city

所以我需要那个城市,以便可以将其传递给iFrame,否则我需要为每个城市制作一个页面...

<a class="button fancybox.iframe" id="offerte" data-fancybox-type="iframe" href="offerte.php?page=city">Offerte aanvragen <span>»</span></a>

像这样的东西行得通,但是我无法以正确的方式进入href!

document.write(document.URL);

如何获取页面链接并将其传递给href?

您需要使用location.href属性。 如果不需要域和协议,则可以使用location.pathname

了解有关MDN Location对象的更多信息。

您提到您在标记中使用了jQuery,因此使用jQuery,您可以通过以下方式进行操作:

$( '#offerte' ).attr( 'href', location.pathname );

如果需要将其作为参数传递,请尝试以下操作:

$( '#offerte' ).attr( 'href', 'offerte.php?page=' + location.pathname );

请在此处使用window.location更多信息http://www.w3schools.com/jsref/obj_location.asp

你可以试试,

document.getElementById("offerte").setAttribute("href",location.href);

请尝试以下代码:

假设网址:example.com/city-> example.com/paris

var u=window.location.href;
var city=u.substring(u.lastIndexOf("/")+1);
document.getElementById("offerte").setAttribute("href","offerte.php?page="+city);

暂无
暂无

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

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