jQuery difference between 'div' and '<

2019-09-13 02:25发布

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条回答
迷人小祖宗
2楼-- · 2019-09-13 03:04

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

查看更多
登录 后发表回答