Where is the Documentation for all of the Javascript HTML Element Constructors?

后端 未结 4 1421
無奈伤痛
無奈伤痛 2020-12-19 16:58

I cannot seem to find it. Thanks!

Example:

$(\"#MySelect\").append(new Option(\"MyOption\", \"MyOption\", true, true));
相关标签:
4条回答
  • 2020-12-19 17:34

    If you don't know / can't find that doc, You can try using the HTML if you know that.

    $("#MySelect").append("<option value='myValue'>MyValue</option>");
    

    or

    $("#MySelect").append("<option value='myValue' selected>MyValue</option>");
    
    0 讨论(0)
  • 2020-12-19 17:39

    The syntax you have looks like it might be jQuery - is it? If so, then append takes an HTML string. You don't have to create HTMLElement subclasses for it.

    This reference from Mozilla will be helpful in understanding the HTML DOM elements.

    This site provides javadoc style documentation on the HTMLElement class and its subclasses.

    0 讨论(0)
  • 2020-12-19 17:42

    The Mozilla Dev Center is the de facto standard documentation site for all things JavaScript.

    option element reference:

    • HTML <option> element
    • HTMLOptionElement interface (which is implemented by the above)
    • W3C HTMLOptionElement interface spec

    Since you're using jQuery, however, there's a better way to construct elements.

    $('#MySelect').append('<option></option>',
    {
        text: 'MyOption',
        value: 'MyOption',
        selected: true
    })
    

    I'm not sure what the last true argument should do - at least in Chrome, new Option('text', 'value', true, true) seems to return the same thing as new Option('text', 'value', true).

    0 讨论(0)
  • 2020-12-19 17:58

    None of the current postings answers the question regarding getting "... Documentation for ALL of the Javascript HTML Element Constructors". The example is explicitly using the " ... new Option ..." constructor.

    This "answer" provides references that do not define any constructors such as new Option().

    The DOM reference provided, should have defined the Option() constructor or at least alluded to it.

    The programmed constructor Option() is NOT HTML and so is NOT defined by the other two references which describe HTML. The tag element, <OPTION> is an HTML defined token so presumably, by implicit precedent only, new Option() is the corresponding equivalent constructor, absorbing <OPTION> property attributions analogously to other dynamic constructs, but where is this formally, explicitly and emphatically stated to define new Option()? In particular, the kind and order of new Option()'s parameters would be defined in such a document.

    Though it's not a constructor proper, the createElement() document method is well defined ( https://developer.mozilla.org/en/DOM/document.createElement ). Using document.createElement("option") with the HTMLOptionElement references in this "answer" completely defines such construction.

    An archaic document that initiated a legacy of confused paradigms between the DOM, HTML and javascript does define new Option() here but is inadequate in the contemporary context.

    0 讨论(0)
提交回复
热议问题