How to check existence of element by id using Mootools
How to check 开发者_开发技巧existence of element by id using Mootools
html:
<div id="foo">some content</div>
javascript
var foo = document.id('foo'); // or $ but should be avoided due to conflicts
// if it returns an Element object, it will be truthy.
if (foo) {
// code for when it exists
}
else {
// code for when it does not.
}
incidentally, this mimics the behaviour of the return value of document.getElementById
which is vanilla js. it can be true for any selector that is meant to return a single Element, like document.getElement('div.login > a.active')
- does not need to be by ID only.
精彩评论