admin管理员组文章数量:1290526
As per jQuery 3.5 documentation, jQuery.trim() is deprecated.
But when I try to use it with jQuery 3.5 it's still working. Should I replace it with the native String#trim function immediately, or can I still use jQuery trim for a while?
As per jQuery 3.5 documentation, jQuery.trim() is deprecated.
But when I try to use it with jQuery 3.5 it's still working. Should I replace it with the native String#trim function immediately, or can I still use jQuery trim for a while?
Share Improve this question edited Dec 23, 2022 at 0:27 Tristan F.-R. 4,2243 gold badges25 silver badges55 bronze badges asked Aug 12, 2020 at 13:16 arunnbioarunnbio 531 gold badge1 silver badge6 bronze badges 3- 5 Deprecated means it's a warning it will be removed in the future, it does not mean it was removed now. – Spencer Wieczorek Commented Aug 12, 2020 at 13:17
- And to answer your question, yes you should use the native function. – Karl-André Gagnon Commented Aug 12, 2020 at 13:18
- String.trim works on all useful browsers – mplungjan Commented Aug 12, 2020 at 13:22
3 Answers
Reset to default 6Deprecated does not mean removed, it means that in a future version there is no guarantee that this method will still exist.
As per the jquery trim docs:
Note: This API has been deprecated in jQuery 3.5; please use the native String.prototype.trim method instead.
Docs for String.prototype.trim
Yes, it is depreciated but the suggested replacement of String.prototype.trim()
can cause issues since it makes the assumption that the object is already a string.
I chose to define the following and turn all calls of $.trim(object)
into String.trim(object)
.
String.trim = String.trim || function (value) {
if (value === null)
return '';
else
return String(value).trim();
};
It appears that there is a change in how this function is now being called. For codes using jQuery 3.5 and below, the usual function call works:
let xString = " This is my string with spaces ";
let xTrimmed = xString.trim();
But for codes using jQuery 3.5 upwards:
let xString = " This is my string with spaces ";
let xTrimmed = $.trim(xString);
Edit:
It appears that jQuery as gone back to its original method: xString.trim(); try any of the two that works.
本文标签: javascriptIs jQuerytrim() deprecatedStack Overflow
版权声明:本文标题:javascript - Is jQuery.trim() deprecated? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741443158a2379034.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论