Correct closing tag in jQuery selector

2019-07-27 09:14发布

问题:

Here's a snippet of code I saw on the web and I'm wondering if the <p/>is a typo or what

 'config' : {

    'content' : $('#topGlobal')
  },

topGlobal.$content= topGlobal.config.content;

topGlobal.$sections = topGlobal.$content.
    find('ul.sections > li');

topGlobal.$side_nav = $('<p/>')
  .attr('id','side_nav ')
  .prependTo(topGlobal.$content);

topGlobal.$item_nav = $('<p/>')
  .attr('id','item_nav')
  .insertAfter(topGlobal.$side_nav);

...

The <p/>is repeated in several additional parts of this code. This may sound like a stupid question and while I haven't tested it, I know <p></p> is correct HTML. But because the author is very well respected JS author, I'm asking. It's probably a typo, but I'd still like to know why the code is selecting the closing tag.

回答1:

<p/> is a shorthand you can use in jQuery to create a new HTML element instance. In this case, a paragraph element.



回答2:

<p/> is equivalent to <p></p> in HTML. It is not a closing tag, which would be </p>.