Jquery href在<;面积>;标签

Jquery href在<;面积>;标签,jquery,Jquery,如果我想根据分配给变量的随机数更改区域标记的href。我该怎么做 我尝试使用公式$('#id').attr('href','link')但对我来说没有效果 这是我的密码: <body> <script> var choose = Math.floor((Math.random()*4)+1); if (choose == 1){ $('#first').attr('href', 'true.html'); $('#second').attr('href', 'fals

如果我想根据分配给变量的随机数更改区域标记的href。我该怎么做

我尝试使用公式
$('#id').attr('href','link')但对我来说没有效果

这是我的密码:

<body>
<script>
var choose = Math.floor((Math.random()*4)+1);


if (choose == 1){
$('#first').attr('href', 'true.html');
$('#second').attr('href', 'false.html');}

else{
$('#first').attr('href', 'false.html');
$('#second').attr('href', 'true.html');}

</script>

<map name="rockpos" id="rockys">
<area shape="rect" id="first" coords="1,1,137,270" href=""     />
<area shape="rect" id="second"coords="208,4,340,273" href=""     />
</map>

</body>

var choose=Math.floor((Math.random()*4)+1);
如果(选择==1){
$('#first').attr('href','true.html');
$('#second').attr('href','false.html');}
否则{
$('#first').attr('href','false.html');
$('#second').attr('href','true.html');}

最简单的方法是:

document.getElementById("#id").setAttribute("href","somelink");

$(document.ready()

试试这个:

<script>
        $(document).ready(function(){
           var choose = Math.floor((Math.random()*4)+1);


           if (choose == 1){
             $('#first').attr('href', 'true.html');
             $('#second').attr('href', 'false.html');}
           else{
             $('#first').attr('href', 'false.html');
             $('#second').attr('href', 'true.html');}
        });
     </script>

$(文档).ready(函数(){
var choose=Math.floor((Math.random()*4)+1);
如果(选择==1){
$('#first').attr('href','true.html');
$('#second').attr('href','false.html');}
否则{
$('#first').attr('href','false.html');
$('#second').attr('href','true.html');}
});

它起作用了,这是工作原理

希望您将脚本添加到head标记中,以便使用jquery

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>


谢谢

将您的代码放入
$(function(){--您的代码--})什么不适合你?我试过拉小提琴,它取代了href。