What does the selector syntax mean in $( “
” ).text( message )

前端 未结 6 1615
旧巷少年郎
旧巷少年郎 2021-01-03 15:50

What does the selector

syntax mean in this code? I\'ve seen selectors like div or #someId but I\'m confused what the
相关标签:
6条回答
  • 2021-01-03 15:57

    $('<div/>') will not select a div from your html, but create a new one. Also it can be written like $('<div></div>'), first one is just a shorthand for second.

    0 讨论(0)
  • 2021-01-03 15:57

    <div /> indicates that the div is a self closing div. It's basically shorthand for $('<div></div>').

    Any browser that supports XHTML supports the self-closing syntax on all elements.

    0 讨论(0)
  • 2021-01-03 16:01

    It means that the jquery function will not actually query for DIV elements but will create an jquery wrapped element of type <div></div> on which you can work on and append to the DOM eventually.

    In your case, the code will create jquery object representing a DIV element, set its innerText to what the message variable contains and prepend it to the element with ID "log".

    0 讨论(0)
  • 2021-01-03 16:08

    It means "create a jQuery-wrapped div element on the fly".

    See http://api.jquery.com/jQuery/

    From the above:

    jQuery( html [, ownerDocument] )

    Description: Creates DOM elements on the fly from the provided string of raw HTML.

    Later...

    When the parameter has a single tag, such as $('<img />') or $('<a></a>'), jQuery creates the element using the native JavaScript createElement() function.

    So basically, it's like doing:

    $(document.createElement("div")).text("blahblah");
    
    0 讨论(0)
  • 2021-01-03 16:14

    It will create a new <div/> element and prepend it to the #log element.

    0 讨论(0)
  • 2021-01-03 16:16

    It creates a new div tag and prepends it to log.

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