jQuery difference between 'div' and '<div/>' when adding class?

廉价感情. 提交于 2019-12-25 06:36:33

问题


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


回答1:


$('<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.



来源:https://stackoverflow.com/questions/15737435/jquery-difference-between-div-and-div-when-adding-class

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