Should I specify the end tag using jquery.append ()?

I am studying someone else's jquery script and I noticed that it opens the tag without closing it, but it also seems like the browser doesn't care (not yet tested with IE)

it is written:

$('#mydiv').append('<ul>')

But nowhere exists

.append('</ul>')

The script does not close the list, but browsers do this automatically (I just made a "check item" in the browser).

Is this a “legitimate” behavior, or should I always close the tag in self-generated javascript content?

+3
source share
2 answers

To do this correctly, you must add:

$('#mydiv').append('<ul></ul>')

, ( , .innerHTML , jQuery), , , ?

$('#mydiv').append('<ul>')

... .innerHTML, createElement, $('<ul>') document.createElement() . , .append(), jQuery, document.createElement ( ).
/ ,

+4

: .

, :

.append('<ul>'),
.append('<ul></ul'), jQuery document.createElement, , .

, jQuery HTML , DOM

update- , . : init ul, createElement. html , buildFragment, .

, , / jQuery - -

$('<ul>').appendTo($target);

2- , , jQuery createElement , append clean, , . , , , jQuery . :

...

} else if ( typeof elem === "string" ) {
  // Fix "XHTML"-style tags in all browsers
  elem = elem.replace(rxhtmlTag, "<$1></$2>");

  ...

UPDATE 3- , , jQuery , append, div. , , HTML, , , , ! , , - .append($('<ul>')), innerHTML

+3

Source: https://habr.com/ru/post/1774825/


All Articles