公告
财富商城
积分规则
提问
发文
2019-06-25 11:52发布
Explosion°爆炸
Is there any different between
$(element).empty() and $(element).text('');
$(element).empty()
$(element).text('');
Which one is better?
Result is the same, but if we look at jQuery text method implementation:
text: function (value) { return jQuery.access(this, function (value) { return value === undefined ? jQuery.text(this) : this.empty().append((this[0] && this[0].ownerDocument || document).createTextNode(value)); }, null, value, arguments.length); },
we will see that $(element).text(''); will actually use .empty internally. Hence latter is a little faster.
.empty
Also from semantic point of view if you want to empty some node you should definetely use .empty method, not text.
text
最多设置5个标签!
Result is the same, but if we look at jQuery text method implementation:
we will see that
$(element).text('');
will actually use.empty
internally. Hence latter is a little faster.Also from semantic point of view if you want to empty some node you should definetely use
.empty
method, nottext
.