语法:
jQuery.contains(container,contained) //返回值是bool类型。
参数:
container:DOM元素作为容器,可以包含其他元素
contained:DOM节点,可能被其他元素所包含
检测下试一试:
jQuery.contains(document.documentElement, document.body)// true
jQuery.contains(document.body, document.documentElement)// false
<!doctype html><title>dom contains 方法 by 司徒正美</title>
<meta charset="utf-8"/>
<meta name="keywords" content="dom contains方法 by 司徒正美" />
<meta name="description" content="dom contains方法 by 司徒正美" />
<script type="text/javascript">
if(!!window.find){
HTMLElement.prototype.contains = function(B){
return this.compareDocumentPosition(B) - 19 >0
}
}
window.onload = function(){
var A = document.getElementById('parent'),
B = document.getElementById('child')
alert(A.contains(B))
alert(B.contains(A))
}
</script>
<h2 style="text-align:center">contains方法</h2>
<div id="parent">
<p>
<strong id="child" >contains方法</strong>
</p>
</div>