I need Test 2 text when I clicked on "Click". I have tried like this ...
$("#clicked").click(function(){
alert( $('this').closest('td').next('td').next('td').text());
})
But In alert there is empty. How I can get Test 2 ?
<tr>
<td> </td>
<td><a id = 'clicked'>Click</a></td>
<td>Test 1</td>
<td><a id = 'clicked2' /> Test 2</a></td>
</tr>
From ment below:
this
means $('#clicked')
. That means when I clicked on the Click
link.
I need Test 2 text when I clicked on "Click". I have tried like this ...
$("#clicked").click(function(){
alert( $('this').closest('td').next('td').next('td').text());
})
But In alert there is empty. How I can get Test 2 ?
<tr>
<td> </td>
<td><a id = 'clicked'>Click</a></td>
<td>Test 1</td>
<td><a id = 'clicked2' /> Test 2</a></td>
</tr>
From ment below:
this
means $('#clicked')
. That means when I clicked on the Click
link.
- What's $(this) for you? – ristapk Commented Aug 16, 2016 at 12:25
- 1 $(this).parent().next().next().text(); – Riad Commented Aug 16, 2016 at 12:26
-
1
Note that your second
a
element shouldn't have the/
in the opening tag. – user1106925 Commented Aug 16, 2016 at 12:26 - 1 @Riad: Why? That looks basically the same. – user1106925 Commented Aug 16, 2016 at 12:27
-
1
Now your
this
is in quotes. Is your actual code like that? If so, that's your issue. If not, your code should work as is. Since you keep changing things, I have a feeling your actual code is different. Is that right? – user1106925 Commented Aug 16, 2016 at 12:55
4 Answers
Reset to default 3you need to use parent
instead of closest
:
$("#clicked").click(function(){
text = $(this).parent().next('td').next('td').text();
alert(text);
})
https://jsfiddle/g6gnog4h/
$('#clicked').click(function(){
console.log($(this).closest('td').next().text());
});
Try this one, as you need to go parent tr and than find the 4th td with the ID of a tag to get the text.
<table><tr>
<td> </td>
<td><a id = 'clicked'>Click</a></td>
<td>Test 1</td>
<td><a id = 'clicked2' /> Test 2</a></td>
</tr>
</table>
<script>
$('#clicked').click(function(){
alert($(this).parents('tr').children().find('#clicked2').text());
});
</script>
You may try like this:
$('#clicked').click(function(){
var val=$(this).siblings().text();
alert(val);
});
siblings() method returns all sibling elements of the selected element.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745470554a4629111.html
评论列表(0条)