から明示的に宣言せずに子要素をロードする親要素を見つけるにはどうすればよいですか?明示的に宣言せずに子要素をロードする親要素を見つける方法
jqueryの、
$(document).ready(function(){
$('.load-child').load_child();
});
(function($) {
$.fn.extend({
load_child: function(options) {
var o = $.extend({
target:'.parent'
}, options);
var $this = this;
var $cm = this.click(function(e) {
var object = $(this);
var path = object.attr('href');
$(o.target).load(path, function(){
$this.child();
});
return false;
});
$this.child = function() {
$('.find-parent').click(function(event){
var object = $(this);
object.parents('.parent').css({background:'red'});
return false;
})
};
return $cm;
}
})
})(jQuery);
インデックスHTML、
<a href="child.php" class="load-child">load child</a>
<div class="container">
<div class="parent">
</div>
</div>
子ページ、
<div>
<div></div>
<div></div>
<div><a href="#" class="find-parent">click me</a></div>
</div>
私は明示的に親の名前を宣言するので、私は上記の.parent
要素を取得することができ、
object.parents('.parent').css({background:'red'});
しかし、親の名前が不明であるか、または常に変更されている場合はどうなりますか?ページを読み込むために使用される要素を検索/トレースするための一般的な方法はありますか?
jsfiddle pageをロードされたページには、div要素の負荷を有することができるので、私はこれを行うことができない、
object.parent().css({background:'red'});
または
object.parents(div).css({background:'red'}); // this will go up to the very top of div element.
。
編集:
あなたはカップル ';' – elclanrs
ありがとうございます。ちょうどそれらを見つけた! lol – laukok