Javascript Javascript将变量连接到url
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7767687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript concatenate variable to url
提问by John Strickler
If i have a variable lets say
如果我有一个变量可以说
var id = //something dynamically generated
then i have <a href="http://localhost/ (the variable id)"
How do i go about this?
我该怎么做?
回答by George Cummins
The concatenation cannot occur within the anchor tag itself, but you can set it like this:
连接不能发生在锚标记本身内,但您可以这样设置:
<a id="my_anchor" href=""></a>
<script type="text/javascript">
// jQuery way
$( "#my_anchor" ).attr( 'href', 'http://localhost/' + id );
// Non-jQuery way
document.getElementById( "my_anchor" ).href = 'http://localhost/' + id;
</script>
回答by John Strickler
Suppose:
认为:
<a id="yourLink" href="http://localhost/ (the variable id)" />
Plain JavaScript
纯 JavaScript
document.getElementById('yourLink').href += id;
or in jQuery
或在 jQuery 中
$('#yourLink').attr('href', function() {
return this.href + id;
});
回答by Domenic
Nobody has pointed out you should always do encodeURIComponent
when you set things like this; otherwise you leave yourself open to script injection attacks:
encodeURIComponent
当你设置这样的东西时,没有人指出你应该总是这样做;否则你会让自己容易受到脚本注入攻击:
Taking @jayp's example:
以@jayp 为例:
<a href="#" id="mylink">My Link</a>
var id = getSomethingDynamic();
var url = "http://localhost/" + encodeURIComponent(id);
document.getElementById("mylink").href = url;
回答by James
Something like this:
像这样的东西:
<a href="#" id="mylink">My Link</a>
var id = // something dynamic
var url = "http://localhost/"+id;
document.getElementById("mylink").href = url;
You don't have to wait for the document to have fully loaded, in most cases as long as the element has been loaded into the DOM you will be fine assuming that your Javascript is placed after the element (like above).
您不必等待文档完全加载,在大多数情况下,只要元素已加载到 DOM 中,假设您的 Javascript 放在元素之后(如上),您就可以了。
Not good practice, but it's misleading to say you "have" to wait for the whole page to be loaded. You don't.
不是好的做法,但说您“必须”等待整个页面加载是一种误导。你没有。
回答by larp
Try this one, I used this
试试这个,我用过这个
<script type="text/javascript">
var id = 2; //something dynamically generated
<a href='http://localhost/edit.php?catID "+id+" '>
<script>
回答by Alan Kuras
Javascript function:
Javascript函数:
function openUrl(id)
{
window.location = "http://localhost/"+id
}
html:
html:
<a href="javascript:void(0)" onclick="openUrl(id)">link</a>
回答by TJHeuvel
You can just retrieve the DOM element, and add something to the href property. Like this:
您可以只检索 DOM 元素,并向 href 属性添加一些内容。像这样:
<script type='text/javascript'>
var id = 5;
window.onload = function()
{
document.getElementById('url').href += id;
}
</script>
<a href='http://localhost/?id=' id='url'>Click me</a>
You have to wait for the document to load in order to get the element, it would not exist otherwise.
您必须等待文档加载才能获取元素,否则它将不存在。