Correct closing tag in jQuery selector

纵饮孤独 提交于 2019-12-13 01:49:45

问题


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>.



来源:https://stackoverflow.com/questions/11040725/correct-closing-tag-in-jquery-selector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!