# elem.isInside()

{% tabs %}
{% tab title="💾 程式" %}

* [replit](https://replit.com/@pegasusroe/elemisInsideselector#script.js)

```javascript
Element.prototype.isInside = function(selector){
    return this.closest(selector) !== null
};
```

💈範例：

```javascript
function $(selector) {
    return document.querySelector(selector)
}

document.body.isInside('html')    // true
document.body.isInside('head')    // false

$('body').isInside('html')        // true
$('#p1').isInside('body')         // true
```

{% endtab %}

{% tab title="📗 參考" %}

* [ ] JavaScript The Definitive Guide (15.3 Scripting Document)
  {% endtab %}

{% tab title="👥 相關" %}

* [..](https://lochiwei.gitbook.io/web/browser/dom/type/element "mention")
  {% endtab %}
  {% endtabs %}
