JavaScript determines if the parameter is a DOM element

Source: Internet
Author: User

Requirements Scenario:
It is sometimes necessary to judge whether a parameter is passed in as a DOM element, or if it is necessary to distinguish between a DOM or a non-dom element.
Example: An extension method MyMethod (domarg,optionsarg), parameters can be passed in (assuming that there are default processing internally), which makes sense for determining whether the parameter domarg is a DOM element.

The analysis code is as follows:

function Isdom (obj) {
vartypeof' object ' )? obj instanceof htmlelement : typeof' object 'typeof' String ';

further extension:
Sometimes the incoming object is not an obj, but a string (for example: Id\class, etc.), which is to preprocess the incoming parameters.
Judgment 1:

var temp=null;
if (typeof obj=== ' string ') {temp=$ (obj) [0];}

Judgment 2:
if (typeof obj=== ' object ') {tempobj=obj;}

Note that the above two judgements must be executed before the Isdom (obj) Method!

JavaScript determines if the parameter is a DOM element

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.