简体   繁体   English

如何替换jQuery $ .trim()?

[英]How to replace jQuery $.trim()?

I'm using jQuery in a website has a polyfill for the built-in String.trim() . 我在一个网站中使用jQuery的网站具有内置String.trim() Site used to run in IE 8 a lot and needed the polyfill, but it doesn't anymore. 该站点过去经常在IE 8中运行,并且需要使用polyfill,但现在不再使用了。 Unfortunately I can't remove the polyfill from the page -- I don't have permissions to touch that and there is no possible way for me to remove it -- so this bit of code runs before anything I can control: 不幸的是,我无法从页面中删除该polyfill -我没有触摸它的权限,而且我也没有办法删除它-因此这段代码在我可以控制的一切之前运行:

String.prototype.trim = function() {
    return this.replace(/^\s\s*/, "").replace(/\s\s*$/, "")
}

Then jQuery comes along and does this, not realizing that the native String.trim has already by messed with: 然后jQuery出现并执行此操作,而没有意识到原生String.trim已经被弄乱了:

// Use native String.trim function wherever possible
trim: core_trim && !core_trim.call("\uFEFF\xA0") ?
    function( text ) {
        return text == null ?
            "" :
            core_trim.call( text );
    } :

    // Otherwise use our own trimming functionality
    function( text ) {
        return text == null ?
            "" :
            ( text + "" ).replace( rtrim, "" );
    },

Up to now this hasn't really been much of a problem, but I'm using the Datatables plugin for jQuery and it has many places where it calls $.trim() on data that isn't a string, like number or arrays. 到目前为止,这实际上并不是什么大问题,但是我使用的是jQuery的Datatables插件 ,它在很多地方都对非字符串数据$.trim()例如数字或数组$.trim()调用$.trim() The native code in IE 11 and Chrome (the browsers we target) knows to just return the value of $.trim(6) , but the polyfill doesn't. IE 11和Chrome(我们定位的浏览器)中的本机代码知道只返回$.trim(6)的值,但$.trim(6)不会。

I tried redefining the the prototype with a function that should work: 我尝试使用应该起作用的功能重新定义原型:

String.prototype.trim = function(){
   if(typeof this.valueOf(this) === 'string'){
       return this.replace(/^\s\s*/, "").replace(/\s\s*$/, "");
   } else {
       return this.valueOf(this);
   }
} 

But that didn't work because jQuery has already extend using the polyfill and further changes to the prototype won't change what jQuery is using. 但这没有用,因为jQuery已经使用polyfill进行了扩展,并且对原型的进一步更改不会改变jQuery的使用方式。

I tried following this thread to redefine $.trim(), but that didn't work. 我尝试按照此线程重新定义$ .trim(),但此操作无效。

Is there a way to return String.prototype.trim() to its native code? 有没有办法将String.prototype.trim()返回其本机代码?

Is there a way to redefine $.trim()? 有没有办法重新定义$ .trim()?

Some other idea I haven't thought of? 我没想到的其他想法?

You can override jQuery core methods 您可以覆盖jQuery核心方法

 (function(){ var string = " TRIM ME "; console.log(jQuery.trim(string)); // Define overriding method. jQuery.trim = function(string){ return string; } console.log(jQuery.trim(string)); })(); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 

Simply override jQuery's $.trim() using String.prototype.trim() , then override String.prototype.trim() with your function: 只需使用String.prototype.trim()覆盖jQuery的$.trim() ,然后使用您的函数覆盖String.prototype.trim()

 var trimText = " Some text "; String.prototype.trim = function() { return this.replace(/^\\s\\s*/, "").replace(/\\s\\s*$/, "") } $.trim = function(string) { return String.prototype.trim(string); } trimText = trimText.trim(); console.log(trimText); 
 <script src="https://code.jquery.com/jquery-3.3.1.js"></script> 

Based on Jack Bashford's answer I came up with this. 基于杰克·巴什福德的回答,我想到了这一点。

String.prototype.trim = function(){
  if(typeof this.valueOf(this) === 'string'){
    return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  } else {
    return this.valueOf(this);
  }
};

$.trim = function(e){
    return String.prototype.trim.call(e);
};

Part of the original problem was that I needed to fix it so that if $.trim(number) or $.trim(array) was called it wouldn't throw and error. 原始问题的一部分是我需要修复它,以便如果调用$ .trim(number)或$ .trim(array)不会引发错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM