Select Children of an object with jQuery
I have this function that I'm using to open a modal dialog that I need to populate开发者_如何学运维 with some information from the 'dblclicked' node:
$(function(){
$(".delete").live('dblclick', function () {
var id = $(this).attr('id');
$('#delID').val(id);
var txt = this.parentNode.children[2].innerHTML;
$("#details").html(txt);
$("#delAssign").dialog('open');
});
});
Example of the html:
<tr>
<td class='delete'></td>
<td></td>
...
<td></td>
</tr>
<tr>
...
</tr>
Right now I'm getting the
var txt = ...
with pure javaScript.
The question is "How can I achieve the same result for the var txt using jQuery?"
You can change it to:
var txt = $(this).siblings(":eq(2)").html();
I think it's the fastest solution.
Like this:
var txt = $(this).parent().children().eq(2).html();
.parent()
selects the parent element.
.children()
selects the children
.eq(2)
selects the element with index 2
within the children
.html()
gets the html content of this element
edit: changed children(':eq(2)')
to children().eq(2)
thanks to Felix Kling
var txt = $(this).siblings().eq(2).html();
Using eq()
as a method instead of passing it as the selector is faster.
Anyway, since you already had the solution in JavaScript, why you want to slow down things using jQuery?
精彩评论