2013-05-21 21 views
0

錨我想訪問anchor標籤在我jquery嵌套如下。我要選擇的每個錨和點擊事件jquery添加到它。jQuery選擇,訪問來自與表

<table id="table1"> 
<tr> 
    <th>Tags</th> 
    <th>ID</th> 
    <th>Batch ID</th> 
    <th>Source</th> 
    <th>Date</th> 
    <th>Details 
     <div id="detailsdiv"> 
     <a href="#" id="default">Default</a>&nbsp;|&nbsp; 
     <a href="#" id="wrap">Wrap</a> 
     </div> 
    </th> 
    <th>Account Name</th> 
    <th>Col15</th> 
    <th>Col16</th> 
    <th>Col17</th> 
    <th>Col18</th> 
    </tr> 

回答

3

使用以下選擇錨標籤:

$("table #detailsdiv a") 

並採用.on()方法應用點擊功能:

$("table #detailsdiv a").on("click", function() { 
    //use this to select the element that has been clicked 
    $(this);   

    //Do click functionality here 
}); 

另外,您也可以使用.click()直接使用jQuery方法:

$("table #detailsdiv a").click(function() { 
    //use this to select the element that has been clicked 
    $(this);   

    //Do click functionality here 
}); 
+0

我有兩個anhor我要訪問兩個 – user2381733

+0

那個選擇r會將您的點擊功能應用於兩個定位標記。 – 97ldave

2

試試這個:

$('#detailsdiv a').click(function(event){ 

    // Cancel the default action (navigation) of the click. 
    event.preventDefault(); 

    // You can get the id of clicked link 
    alert(this.id); 

    // Your code goes here.. 
}); 
0
$('table tr th a').each(function(){ 
    $(this).click(function(e){ 
     e.preventDefault(); 
     //your function here; 
    }); 
}); 
1

嘗試

$("#detailsdiv a").click(function() { 
      // add function And 
      $(this) // add function for clicked tag 
     }); 
0

呦也可以試試這個:

$('table #detailsdiv a').click(function(e){ 
    alert(this.id); 
});