事先,那麼我會用
$("#starting_point").parent().parent().parent().parent().find("#ending_point");
或者用.parentsUntil()(https://api.jquery.com/parentsUntil/)做點什麼。 (好的,不是一個漂亮的解決方案,但肯定有效。)
如果您不知道層次結構(它是動態創建的),但您確定,該#starting_point比#ending_point「低」,那麼您可以創建一個遞歸函數,檢查你正在尋找的類型的每個級別。在函數中使用
// start_obj is the jQuery object you start your recursive function from
// end_type is the type of element you are looking for in the hierarchy (e.g. input[ type="text" ] in your case
function get_sibling_on_other_level(start_obj, end_type) {
var grampy = start_obj.parent().parent();
if (grampy.find(end_type).length > 0) {
return grampy.children(end_type);
} else {
// start the iteration "one level higher"
// actually you should stop at "body" level - if you did not find
// what you were looking for, then it's not in that branch of hierarchy
get_sibling_on_other_level(start_obj.parent(), end_type);
}
}
其他jQuery的方法:https://api.jquery.com/children/
這正是我want.Thank你:) – AmudhaVigneshwaran