开发者

jQuery: select "this" child elements

开发者 https://www.devze.com 2023-01-23 16:33 出处:网络
I\'m really going crazy of this particular obstacle, the issue in hand is that I have a very simple table with rows and all. what I\'m trying to accomplish is that when you click on a row then this sc

I'm really going crazy of this particular obstacle, the issue in hand is that I have a very simple table with rows and all. what I'm trying to accomplish is that when you click on a row then this script is suppose to read the link that's stored in the last td in the very same row and then direct you there.

What I've come up with so far is this

开发者_运维技巧 $('tr td').click(
     function (){
        alert($(this+':parent td:last-child a:first').attr('href'));
     }
 );

I've tried 100 of different approaches I either get an error/undefined or I only get the result of the last/first row the rows in-between doesn't work as supposed to.

All help is really appreciated

table looked as following

http://www.jsfiddle.net/xK7Mg/1/


I think you meant to do this:

$('tr td').click(function() {
   window.location.href = $(this).parent().find('td:last-child a:first').attr('href');
});


What you need is this I think:

$('tr').click(function(){
    window.location = $(this).find('td:last a:first').attr('href');
});

This script will cause each table row, when clicked, redirect you to the location referenced in the first anchor element in the last table cell.


Use .siblings() in this case, though .parent().children() also works, like this:

$('tr td').click(function() {
  window.location.href = $(this).siblings('td:last').find('a').attr('href');
});

Take it one step further though, don't attach a click handler to every cell, use .delegate() to attach one for the entire table, like this:

$('#tableID').delegate('td', 'click', function() {
  window.location.href = $(this).siblings('td:last').find('a').attr('href');
});

You can try it out here.


$('tr').click(
     function (){
        window.location = $(this).find("td:last a:first").attr('href');
     }
 );


http://jsfiddle.net/ppw8z/

 $('tr').click(function() {
    window.location = $(this).find('td>a').attr('href');    
 });
0

精彩评论

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