问题
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