jQuery 将变量值传递给锚标记中的 href 参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17963058/
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
passing variable value to href argument in anchor tag
提问by AKIWEB
how to pass the variable value to href argument in anchor tag.
如何将变量值传递给锚标记中的 href 参数。
<body>
<script>
var id = "10";
$('a_tag_id').attr('href','http://www.google.com&jobid='+id);
</script>
<a id="a_tag_id">something_here</a>
</body>
I want anchor tag to look like this after the above code is executed.
我希望锚标记在执行上述代码后看起来像这样。
<a href="http://www.google.com&jobid=10">something_here</a>
But somehow the above code is not working. Is there anything wrong I am doing?
但不知何故,上面的代码不起作用。我做错了什么吗?
回答by Alessandro Minoccheri
You have miss #
in jQuery selector, and insert the code inside the document.ready to make that your script work when the page is ready
您错过#
了 jQuery 选择器,并在 document.ready 中插入代码以使您的脚本在页面准备好时工作
Try this:
尝试这个:
<script>
$(document).ready(function(){
var id = "10";
$('#a_tag_id').attr('href','http://www.google.com&jobid='+id);
});
</script>
回答by hungerstar
You're missing the #
for the id. Try using $('#a_tag_id')
. Use an ?
before your first query string variable instead of &
.
您缺少#
id 。尝试使用$('#a_tag_id')
. ?
在第一个查询字符串变量之前使用 ,而不是&
.
回答by Dhaval Marthak
Use #
for id
selctor
使用#
的id
selctor
$('#a_tag_id').attr('href','http://www.google.com&jobid='+id);
^
回答by MACMAN
Modify the jquery like this
像这样修改jquery
$('#a_tag_id').attr('href','http://www.google.com&jobid='+id);
回答by Uooo
When your Javascript is executed, the a
tag might not exist yet.
当您的 Javascript 被执行时,a
标签可能还不存在。
Use:
用:
$(document).ready(function(){
var id = "10";
$('#a_tag_id').attr('href','http://www.google.com&jobid='+id);
});
Also, it should be #a_tag_id
to match the id.
另外,应该#a_tag_id
匹配id。
回答by Abhitalks
$('#a_tag_id').attr('href','http://www.google.com?jobid='+id);
The selector should be for id '#', and the querystring starts with "?".
选择器应该用于 id '#',并且查询字符串以“?”开头。
BTW: Isn't this question following up on your last question: Pass an id to anchor tag?
顺便说一句:这个问题不是在跟进您的最后一个问题:将 id 传递给锚标记吗?