0
4

8 回答 8

4

这不是最好的解决方案。为获得最佳结果,您可能需要重组 html 并向链接或其父级添加某种类和 ID 以识别它们。但这会起作用

对于第一个链接

$("a:eq(0)").click(function() {
location.href="first.location";
return false;
});

对于第二个链接

 $("a:eq(1)").click(function() {
 location.href=="second.location";
 return false;
 });
于 2013-06-25T08:55:44.997 回答
2

如果您href在标记中设置 ,则不需要JQueryor Javascript

<a href="first.location">.zip file, first link
</a>

<a href="second.location">.tar.gz file, second link
</a>
于 2013-06-25T08:56:07.843 回答
1

就像已经建议的那样,最好的方法是为这些 a 标签设置不同的 id。但是,如果由于某种原因您不想分配 ids(您到底为什么要这样做?)您可以执行以下操作:

将锚标签包装在一个 div 中并给它一个像这样的 id

 <div id="myDiv">
  <a href="#">First Link</a>
  <a href="#">Second Div</a>
 </div >

然后使用 jQuery 进行链接:

<script>
 $(function(){
   $("myDiv").children(a:first-child).click(function(){
      // Do stuff here
   });

   $("myDiv").children(a:last-child).click(function(){
      // Do stuff here
   });
 });
</script>
于 2013-06-25T09:30:20.620 回答
1

您可以在此处使用:eq() 选择器,例如:

// Clicking the first link
$("a:eq(0)").click(function () {
    location.href = "first.location";
    return false;
});

// Clicking the second link
$("a:eq(1)").click(function () {
    location.href = "second.location";
    return false;
});
于 2013-06-25T08:55:30.373 回答
0

$("a:eq(0)").click(function() { location.href="first.location"; return false; });

$("a:eq(1)").click(function() { location.href=="second.location"; return false; });

于 2013-06-25T09:51:29.233 回答
0

在 html(href) 中给出链接

$("a").click(function()
{
    location.href = $(this).attr('href');
    return false;

});
于 2013-06-25T08:56:46.200 回答
0

我认为这可能会有所帮助:

<a id="first" href="">.zip file, first link</a>
<script>
  $("first").click(function() {
    location.href="first.location";
    return false;
  });
</script>

<a id="second" href="">.tar.gz file, second link </a>

<script>
  $("second").click(function() {
    location.href=="second.location";
    return false;
  });
</script>
于 2013-06-25T08:55:30.277 回答
0

您可以在链接中引入 id 属性。然后根据元素的id触发事件。

<a href="" id='link1'>.zip file, first link
</a>
<script>
$("#link1").click(function() {
location.href="first.location";
return false;
});
</script>

<a href="" id='link2'>.tar.gz file, second link
</a>

 <script>
 $("#link2").click(function() {
 location.href=="second.location";
 return false;
 });

 </script>
于 2013-06-25T08:55:52.210 回答