Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript将变量连接到url_Javascript_Jquery - Fatal编程技术网

Javascript将变量连接到url

Javascript将变量连接到url,javascript,jquery,Javascript,Jquery,如果我有一个变量,让我们说 var id = //something dynamically generated then i have <a href="http://localhost/ (the variable id)" var id=//动态生成的内容 然后我有您可以检索DOM元素,并向href属性添加一些内容。像这样: <script type='text/javascript'> var id = 5; window.onload = function()

如果我有一个变量,让我们说

var id = //something dynamically generated 
then i have <a href="http://localhost/ (the variable id)"
var id=//动态生成的内容

然后我有您可以检索DOM元素,并向href属性添加一些内容。像这样:

<script type='text/javascript'>

var id = 5;

window.onload = function()
{
    document.getElementById('url').href += id;
}

</script>

<a href='http://localhost/?id=' id='url'>Click me</a>

var-id=5;
window.onload=函数()
{
document.getElementById('url')。href+=id;
}
您必须等待文档加载才能获取元素,否则它将不存在。

类似以下内容:

<a href="#" id="mylink">My Link</a>

var id = // something dynamic
var url = "http://localhost/"+id;

document.getElementById("mylink").href = url;
<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>

var id=//动态对象
变量url=”http://localhost/“+id;
document.getElementById(“mylink”).href=url;
您不必等待文档完全加载,在大多数情况下,只要元素已经加载到DOM中,您就可以假设Javascript放在元素之后(如上所述)


这不是一个好的做法,但如果说您“必须”等待整个页面加载,则会产生误导。不需要。连接不能发生在锚标记本身中,但可以如下设置:

<a href="#" id="mylink">My Link</a>

var id = // something dynamic
var url = "http://localhost/"+id;

document.getElementById("mylink").href = url;
<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>

//jQuery方式
$(“#我的锚”).attr('href','http://localhost/“+id);
//非jQuery方式
document.getElementById(“我的锚”).href='http://localhost/“+id;
假设:

<a id="yourLink" href="http://localhost/ (the variable id)" />
或者在jQuery中

$('#yourLink').attr('href', function() {
  return this.href + id;
});
Javascript函数:

function openUrl(id)
{
 window.location = "http://localhost/"+id
}
html:


没有人指出,当您设置这样的事情时,您应该始终执行
encodeURIComponent
;否则,您可能会受到脚本注入攻击:

以@jayp为例:

<a href="#" id="mylink">My Link</a>

var id = getSomethingDynamic();
var url = "http://localhost/" + encodeURIComponent(id);

document.getElementById("mylink").href = url;

var id=getSomethingDynamic();
变量url=”http://localhost/“+组件(id);
document.getElementById(“mylink”).href=url;

试试这个,我用过这个

<script type="text/javascript">
    var id = 2; //something dynamically generated
    <a href='http://localhost/edit.php?catID "+id+" '>
<script>

var-id=2//动态生成的东西

如果您将
标记放在
标记之后,则会发生这种情况。此外,重写
window.onload
是非常糟糕的做法-1@Domenic这是一种个人偏好,我更喜欢把所有CSS/Javascript放在上面。重载
window.onload
并不是一个坏习惯,如果使用jQuery或类似的工具,肯定会有一些函数来处理onload请求。这是唯一的方法。我想您可以使用AddEventListener,但这会使示例变得不那么简洁,因为它存在一些跨浏览器问题。是的,在2011年使用DOM0事件肯定是一种糟糕的做法。你正在击败任何想使用DOM0事件的人。可惜这些新奇的DOM事件不能跨浏览器兼容。我仍然保持这种方式,OP应该知道这不是生产就绪代码。这取决于你在做什么。编码元件编码?而且&他可能想把它作为他的弦乐作品的一部分。我们不知道他到底想做什么或更广泛的背景,因此直接回答问题而不是猜测似乎更明智。