'div'和'< div />'之间的jQuery差异何时添加类? [英] jQuery difference between 'div' and '<div/>' when adding class?

查看:116
本文介绍了'div'和'< div />'之间的jQuery差异何时添加类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在分析一个插件,但我意识到作者使用:

  $('< div />') .addClass( '样品片'); 

代替以下内容

  $( 'DIV')addClass( '样品片'); 

< div /> < a /> ......它似乎不是一个有效的html标记。



我似乎无法找到任何解决方案,因为谷歌不允许我在网上搜索运营商.....

解决方案 $('< div />')。addClass('sample-piece'); 创建一个新的 div 元素并添加class 样本块。新创建的div当时不在dom树中,您可能需要将其附加到其他元素。



$('div ').addClass('sample-piece'); 添加类样本块到所有 div dom树中的元素。

总结:

$('< div />')会创建一个新的 div 元素。

$('div')选择所有 div 元素。


I was analyzing a plugin but I realize the author uses:

$('<div/>').addClass('sample-piece');

instead of the following

$('div').addClass('sample-piece');

what is the meaning of <div/>, <a/>...... it doesn't seem to be a valid html tag.

I can't seem to find any solution for this as google doesn't allow me from searching operator online.....

解决方案

$('<div/>').addClass('sample-piece'); create a new div element and add class sample-piece to it. The new created div is not in the dom tree at that time, you may need to append it to some other element.

$('div').addClass('sample-piece'); add class sample-piece to the all the div elements in the dom tree.

summarize:

$('<div/>') creates a new div element.

$('div') selects all the div elements.

这篇关于'div'和'&lt; div /&gt;'之间的jQuery差异何时添加类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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