jQuery可以使用结束标签作为选择器吗? [英] Does jQuery can use an end tag as a selector?

查看:97
本文介绍了jQuery可以使用结束标签作为选择器吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很喜欢jQuery。我浏览这个使用< div /> 作为选择器的jQuery代码:

 code> $(< div />)。html(someString).text(); 

首先,我想知道是否使用合法的标签,因为 div 有一个结束标签:< / div>



但是我的主要问题 - jQuery是否可以使用一个结尾标签作为选择器,这样的代码是什么?



这些代码将会做什么?我做了一些研究,这里 meagar 说:


没有像结束标签这样的东西DOM。标签,关闭或其他方面,是您的标记的组成部分,DOM只有元素。


但是jQuery将结束标记视为引用像这样的DOM元素会对待一个开头标签?



非常感谢。

解决方案

请参阅这些:


  1. $(div)其选择器可以选择页面中的所有div。

  2. $(< div>)这创建一个dom节点< div>< / div> 因为< tags>

  3. $(< div />) $(< div>< / div>) / code>与 2相同。

所以在javascript里有一个方法称为 document.createElement(tagName),用于在页面中创建新元素。



所以基本上jQuery有一个不同的语法,但是 c> document.createElement(tagName) p>




对于您的评论:



  var div = $('< div />')。html('< p> Hi !!!< / p>')。 //放一个html字符串并获取it.document.body.textContent = div的文本内容; //将文本内容放在文档正文中。 

  ; script src =https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js>< / script>  


I am pretty new in jQuery. I cam across this jQuery code that uses <div /> as a selector:

$("<div />").html(someString).text();

First of all, I would like to know if it is a legitimate tag to use, since div has an end tag: </div>.

But my main question - does jQuery can use an end tag as a selector and what would be the result of such code?

What will do such code? I did some research, and here, meagar say:

There is no such thing as a "closing tag" in the DOM. Tags, closing or otherwise, are a component of your markup, the DOM has only elements.

but does jQuery treat an end tag as reference to a DOM element like it treats an opening tag?

Thank you very much.

解决方案

See these:

  1. $("div") its a selector which selects all the divs in the page.
  2. $("<div>") This creates a dom node <div></div> because of <tags>.
  3. $("<div />") and $("<div></div>") is also same as 2.

So, in javascript there is a method called document.createElement(tagName), which is used to create new elements in the page.

So basically jQuery has a different syntax but document.createElement(tagName) is used behind the scenes.


For you comment:

var div = $('<div/>').html('<p>Hi!!!</p>').text(); // put a html string and get the text content of it.

document.body.textContent = div; // put the text content in the doc body.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

这篇关于jQuery可以使用结束标签作为选择器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆