Javascript将变量连接到url [英] Javascript concatenate variable to url

查看:70
本文介绍了Javascript将变量连接到url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个变量,请说

If i have a variable lets say

var id = //something dynamically generated 
then i have <a href="http://localhost/ (the variable id)"

我怎么去关于这个?

推荐答案

连接标签本身不能发生连接,但你可以这样设置:

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>

这篇关于Javascript将变量连接到url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆