开发者

Jquery css selector using regular expressions

开发者 https://www.devze.com 2023-01-17 03:41 出处:网络
I have a DYNAMICALLY generated Navigation div <div id=\"WebPartWPQ9\" > <table class=\"xm-ptabarea\">

I have a DYNAMICALLY generated Navigation div

<div id="WebPartWPQ9" >
 <table class="xm-ptabarea">
  <tbody>
   <tr>
    <td nowrap="" class="tab"><a href="/somelink.aspx">Overview</a></td>
    <td nowr开发者_StackOverflow中文版ap="" class="tab"><a href="/anothersomelink.aspx">Financials</a></td>
    <td nowrap="" class="tab"><a href="/somemorelink.aspx">Newsfeed</a></td>
   </tr>
  </tbody>
 </table>
</div>

Since there are no unique IDs, using jquery, i want to be able to attach an event to the "financial" tab link (using regular expressions selectors maybe?)

The value of the link will always be "Financials"


It's super easy to select that <a> element using the :contains selector:

var $financialsLink = $('#WebPartWPQ9').find('td.tab > a:contains(Financials)');


$('#WebPartWPQ9 a:contains(Financials)')


Because it is dynamically generated, you can use .live() to handle events on elements created after the DOM loads.

$('#WebPartWPQ9 td.tab a:contains(Financials)').live('click', function() {
    // run code
});

Or if the #WebPartWPQ9 is present when the page loads, I'd probably use .delegate() instead.

$('#WebPartWPQ9').delegate('td.tab a:contains(Financials)', 'click', function() {
    // run code
});

Both of these use the :contains selector to target the one with "Financials".


You can use the :contains filter selector and live method like this:

$('.tab a:contains("Financials")').live('click', function(){
  // your code...
});
0

精彩评论

暂无评论...
验证码 换一张
取 消