Correct closing tag in jQuery selector

2019-07-27 09:21发布

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.

2条回答
别忘想泡老子
2楼-- · 2019-07-27 10:01

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

查看更多
三岁会撩人
3楼-- · 2019-07-27 10:07

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

查看更多
登录 后发表回答