Javascript 更改href链接的内容

Javascript 更改href链接的内容,javascript,href,Javascript,Href,以下代码是一个测试件。通常链接中的href指向“http://www.google.com/,但attr应将其更改为引用http://maps.google.com“但是,这个参照系没有改变。有人能告诉我为什么它不起作用吗?谢谢 <html> <head> <script type="text/javascript"> $("a#changeme").attr('href', 'http:\/\/maps.google.com/'); </script

以下代码是一个测试件。通常链接中的href指向“http://www.google.com/,但attr应将其更改为引用http://maps.google.com“但是,这个参照系没有改变。有人能告诉我为什么它不起作用吗?谢谢

<html>
<head>
<script type="text/javascript">
$("a#changeme").attr('href', 
'http:\/\/maps.google.com/');
</script>
</head>
<body>
<div class="content">
<p>Link to <a href="http://www.google.com/" 
id="changeme">Google</a>
in the content...</p>
</div>
</body>
</html>

$(“a#changeme”).attr('href',
“http:\/\/maps.google.com/”);
链接到
在内容中

  • 未加载jQuery
  • 如果是,则必须将其包装在处理程序中
  • 这可以在没有jQuery的情况下完成
  • 代码:

    onload
    处理程序与DOMContentLoaded处理程序并不完全相同,但它有更好的支持,在这里可能是首选。或者,您可以将
    块移动到
    的末尾,然后在不使用任何
    onload
    处理程序的情况下使用该方法:

    <body>
    <div class="content">
    <p>Link to <a href="http://www.google.com/" id="changeme">Google</a>
    in the content...</p>
    </div>
    <script type="text/javascript">
      // This code is placed after the element, so the reference does exist now.
      // In the head, the same code will throw an error, because the body doesn't
      // even exist.
      document.getElementById("changeme").href = 'http://maps.google.com/';
    </script>
    </body>
    
    
    链接到
    在内容中

    //此代码放在元素之后,因此引用现在确实存在。 //在头部,同样的代码会抛出一个错误,因为身体不会 //甚至存在。 document.getElementById(“changeme”).href='http://maps.google.com/';
    脚本位于标题中,因此在加载其他内容之前执行脚本(如果jQuery处于活动状态,则看不到对它的引用)。您应该将其放入函数中,然后稍后调用它(例如,通过
    onload
    或计时器)。我还可以在浏览器中考虑一个安全功能,以防止站点在单击链接之前对链接进行操作。

    以下是正确的jQuery方法的代码(使用文档就绪)

    
    $(文档).ready(函数(){
    $(“#changeme”).attr('href','http:\/\/maps.google.com/');
    });
    链接到
    在内容中


    为什么不直接使用
    #changeme
    作为选择器?ID选择器是最快的。选择器向后运行——因此它会先查找#changeme,然后只过滤出锚点……但如果您有唯一的ID,为什么要这样做?为什么要使用
    text/javascript
    ?所有现代浏览器解释脚本标记时都没有将
    type
    属性设置为Javascript。这是一项非常古老的技术。现在我来谈谈我问题的症结所在。我需要更改的参考是下面脚本中的XXX位。Soory忘记了脚本,但我尝试了你的建议,我认为它解决了我的问题。但是需要彻底检查一下。再次感谢,谢谢,是的,我现在明白了。但安全功能是什么?我不确定,但例如,您可以在状态栏中有一个链接,上面写着“”,但当您单击它时,它会被设置为“”。但如前所述,这更可能是由于您编写脚本的方式(在函数之外)。
    <body>
    <div class="content">
    <p>Link to <a href="http://www.google.com/" id="changeme">Google</a>
    in the content...</p>
    </div>
    <script type="text/javascript">
      // This code is placed after the element, so the reference does exist now.
      // In the head, the same code will throw an error, because the body doesn't
      // even exist.
      document.getElementById("changeme").href = 'http://maps.google.com/';
    </script>
    </body>
    
    <html>
    <head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function() {
    $("#changeme").attr('href', 'http:\/\/maps.google.com/');
    });
    </script>
    </head>
    <body>
    <div class="content">
    <p>Link to <a id="changeme" href="http://www.google.com/">Google</a>
    in the content<a href="test">test</a>...</p>
    </div>
    </body>
    </html>