簡體   English   中英

如何以不同的方式調用jquery函數

[英]how to call jquery function with different <a href=“”>

我想調用2個不同的jquery函數來隱藏鏈接

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

如何調用這兩個功能,以便我第一個單擊第一個鏈接,第二個單擊第二個鏈接?

謝謝你

這不是最佳解決方案。 為了獲得最佳結果,您可能需要重組html並向鏈接或其父級添加某種類和ID,以標識它們。 但這會起作用

對於第一個鏈接

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

對於第二個鏈接

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

如果在標記中設置href ,則不需要JQueryJavascript

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

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

您可以在此處使用: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;
});

就像已經建議的那樣,最好的方法是為這些標簽使用不同的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>

我認為這可能會有所幫助:

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

您可以在鏈接中引入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>

在html(href)中提供鏈接

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

});

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM