将变量值传递给锚标记中的href参数 [英] passing variable value to href argument in anchor tag
本文介绍了将变量值传递给锚标记中的href参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
< body> 如何将变量值传递给锚标记中的href参数。
< 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>
在上面的代码执行后,我希望锚标记看起来像这样。
< a href =http://www.google.com& jobid = 10> something_here< / a>
但上面的代码不起作用。
在jQuery选择器中,并将代码插入到document.ready中,以使您的脚本在页面准备就绪时工作。试试这个: < script>
$(document).ready(function(){
var id =10;
$('#a_tag_id').attr('href','http:// www .google.com& jobid ='+ id);
});
< / script>
how to pass the variable value to href argument in anchor tag.
<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?
解决方案
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
Try this:
<script>
$(document).ready(function(){
var id = "10";
$('#a_tag_id').attr('href','http://www.google.com&jobid='+id);
});
</script>
这篇关于将变量值传递给锚标记中的href参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文