var _div = $("<div id='ds-tree-expand'/>").appendTo( "#ds-subject-tree" );
var _cur_token = "susmita";
var _next_token = "sudipta";
for(var i=0; i<5; i++){
var _1st_level = $( "<p id='acc_trigger' class='active collapsed'>" + _cur_token + "</p>" ).appendTo(_div);
var _2nd_level = $( "<p id='2nd_trigger' class='active collapsed'>" + _next_token + "</p>" ).appendTo(_1st_level);
}
var _2nd_ch = _div.children().children();
现在我想访问每个_2nd_ch
元素的父。
我试过了:
$(_2nd_ch[1]).parent();
这不起作用。如何访问_2nd_ch
数组的每个项目的父?
答案 0 :(得分:0)
看到这可能对您有所帮助。 http://jsfiddle.net/z8a0234b/您可以使用_nth
访问任何节点。
var _div = $("<div id='ds-tree-expand'/>").appendTo( "#ds-subject-tree" );
var _cur_token = "Parent";
var _next_token = "Child";
for(var i=0; i<5; i++){
var _1st_level = $( "<p id='acc_trigger' class='active collapsed'>" + _cur_token + " "+ i +"</p>" ).appendTo(_div);
var _2nd_level = $( "<p id='2nd_trigger' class='active collapsed'>" + _next_token + " "+ i +"</p>" ).appendTo(_1st_level);
}
var _nth = 2;/*2dn node, for Parent 1*/
var _nth_parent = $("#ds-tree-expand > #acc_trigger:nth-child("+_nth+")").html();
_nth_parent = _nth_parent.split("<p"); _nth_parent = _nth_parent[0]; /*filter to trace only words no tag*/
var _nth_child = $("#ds-tree-expand > #acc_trigger:nth-child("+_nth+") > p").html();
alert(_nth_parent +" "+ _nth_child);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<div id="ds-subject-tree"></div>