简体   繁体   English

将变量值传递给锚标记中的href参数

[英]passing variable value to href argument in anchor tag

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? 我做错什么了吗?

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>

DEMO 演示

You're missing the # for the id. 您缺少ID的# Try using $('#a_tag_id') . 尝试使用$('#a_tag_id') Use an ? 使用? before your first query string variable instead of & . 在第一个查询字符串变量而不是&

Use # for id selctor #用作id选择器

$('#a_tag_id').attr('href','http://www.google.com&jobid='+id);
   ^

Fiddle Demo 小提琴演示

像这样修改jQuery

 $('#a_tag_id').attr('href','http://www.google.com&jobid='+id);

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。

$('#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传递给锚标记吗?

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

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