2013-06-25 62 views
0

I want to call 2 different jquery functions to hide links如何調用不同<a href="">

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

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

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

</script> 

How can I call the 2 functions so that I call the first one clicking the first link and the second one clicking the second link?

Thanks a lo

+1

你最好給那些錨定一個id,並用它來綁定一個點擊處理器給他們 – billyonecan

+0

你可能也想給你的'href'sa'#'值 –

+0

你在做什麼事情處理器內部?因爲事實上,你根本不需要事件處理程序。 –

回答

0

I think this might help:

<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> 
1

You can use the :eq() Selector這裏jQuery的功能,如:

// 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; 
}); 
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; 
}); 
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> 
2

如果您在標記設置href沒有必要JQueryJavascript。在HTML(HREF)

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

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

給鏈接

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

}); 
1

就像它已經被建議做的最好的方式,是有不同的ID對它們的標籤。但是,如果由於某種原因,你不想分配ID(?爲什麼地球上,你會做到這一點),你可以做到以下幾點:在一個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> 
0

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

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