简体   繁体   English

我必须用jquery.append()指定endding标签吗?

[英]Must I specify endding tag with jquery.append()?

I am studying somebody else jquery script, and I noticed he is opening a tag without closing it, but it also seems that browsers does not care (Not yet tested with IE) 我正在研究别人的jquery脚本,我注意到他正在打开一个标签而没有关闭它,但它似乎也不关心浏览器(尚未测试IE)

it is written : 这个已经写完了 :

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

But there is nowhere a 但是无处可去

.append('</ul>')

The script does not close the list, but browsers do it automatically (I just did an 'inspect element' in the browser). 该脚本不会关闭列表,但浏览器会自动执行此操作(我只是在浏览器中执行了“检查元素”)。

Is that a 'legal' behavior, or one should always close a tag in a javascript autogenerated content ? 这是一种“合法”行为,还是应该始终关闭javascript自动生成内容中的标记?

To do it properly, you should be appending: 要正确地做,你应该追加:

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

Yes browsers will handle it (specifically the .innerHTML implementation handles it, not jQuery), at least the major ones, but why not be safe in all cases and use valid markup? 是的浏览器会处理它(特别是.innerHTML实现处理它, 而不是 jQuery),至少是主要的,但为什么在所有情况下都不安全并使用有效的标记?

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

... still calls .innerHTML , not createElement , only in $('<ul>') is document.createElement() called. ... 仍然调用.innerHTML ,而不是createElement ,只在$('<ul>')中调用document.createElement() As I said originally, the browser handles it with .append() , not jQuery and not document.createElement (which doesn't take syntax like this anyway). 正如我之前所说,浏览器使用.append()处理它, 而不是 jQuery,而不是 document.createElement (它不会采用这样的语法)。
You can see test/play with what I mean here 您可以在这里看到我的意思

Short answer: you should. 简短的回答:你应该。

Long answer that lead to the short answer: 简短的回答导致简短的回答:

When you say .append('<ul>') , 当你说.append('<ul>')
or even .append('<ul></ul') , behind the scenes jQuery calls document.createElement and the browser knows what to do. 甚至.append('<ul></ul') ,在幕后jQuery调用document.createElement ,浏览器知道该怎么做。

It's not like jQuery actually puts that string of HTML anywhere, but rather parses it and creates the necessary DOM elements 它不像jQuery实际上将HTML字符串放在任何地方,而是解析它并创建必要的DOM元素

UPDATE- As Nick pointed out, this might not always be the case. 更新 - 正如尼克指出的那样,情况可能并非总是如此。 Relevant source: init If you pass it just ul , it just calls createElement . 相关来源: init如果只传递ul ,它只调用createElement If the html string is more complicated, it will go into buildFragment which is more complicated than that. 如果html字符串更复杂,它将进入buildFragment ,这比那更复杂。

Based on this, I would say the best/fastest way to create a single element thru jQuery, is to do something like 基于此,我想说通过jQuery创建单个元素的最佳/最快方法是做类似的事情

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

UPDATE 2- So apparently jQuery only calls createElement in some methods, but append ends up calling clean which has a regex that closes tags. 更新2-所以显然jQuery只在某些方法中调用createElement ,但append最终调用clean ,它有一个关闭标签的正则表达式。 So either way, you're safe, jQuery saves you as usual. 无论哪种方式,你都是安全的,jQuery会像往常一样保存你。 Relevant source: 相关来源:

...

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

  ...

UPDATE 3- So it turns out jQuery doens't fix anything for you when you call append , and it just injects the string into a temporary div element. 更新3-所以事实证明,当你调用append ,jQuery不会为你修复任何东西,它只是将字符串注入临时div元素。 Seems like most browsers know how to deal with the HTML even if not closed properly, but to be save it's probably best to close it yourself! 似乎大多数浏览器都知道如何处理HTML,即使没有正确关闭,但要保存它可能最好自己关闭它! Or if you're feeling lazy, do something like .append($('<ul>')) which doesn't use innerHTML 或者,如果你感到懒惰,做一些像.append($('<ul>'))这样不使用innerHTML的东西

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM