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

时间:2022-11-27 17:28:31

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?

但不知何故上面的代码不起作用。我在做什么事吗?

6 个解决方案

#1


2  

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

#2


1  

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')。用?在您的第一个查询字符串变量而不是&之前。

#3


1  

Use # for id selctor

使用#作为id selctor

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

Fiddle Demo

#4


1  

Modify the jquery like this

像这样修改jquery

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

#5


0  

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。

#6


0  

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

#1


2  

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

#2


1  

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')。用?在您的第一个查询字符串变量而不是&之前。

#3


1  

Use # for id selctor

使用#作为id selctor

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

Fiddle Demo

#4


1  

Modify the jquery like this

像这样修改jquery

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

#5


0  

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。

#6


0  

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