JavaScript 本身是不支持这种查找的, 因为"子对象"只是父对象中的一个引用, 它也可以被其它对象引用, 这样一个"子对象"就可能有多个"父对象". 能实现的是在代码运行中获取它的上级对象.
var Obj = function(){
this.child = {
parent: {},
get_parent: function()
{
return this.parent
}
}
this.init = function()
{
this.child.parent = this
}
this.init()
}
var o = new Obj
console.log(o.child.get_parent())
jquery中parent()可以获取父级元素,所以获得某元素父级的父级可以使用
$(selector).parent().parent()示例如下
创建Html代码及css样式
<div class="class1">class1
<div class="class2">
class2
<div class="class3">
class3
</div>
</div>
</div> div{padding:10px 20pxborder:4px solid #ebcbbe}
div.class1{width:200pxheight:120px}
编写jquery代码
$(function(){$("div.class3").click(function() {
obj = $(this).parent().parent()
alert(obj.prop('class'))
})
})
查看效果
$("#floatDiv").before("<div class=\"floatDiv\" id=\"floadDiv"+peo.value+"\"><div class=\"selectDiv\">"+peo.nextSibling.innerText+"</div><div class=\"selectCancle\"><a href=\"javascript:closeDiv(this)\">×</a></div></div>")}else if(!peo.checked){
var divID ="floatDiv"+peo.value
$("#"+divID).remove()
}
}