繁体   English   中英

自定义大写字符串方法,jQuery和Select2的问题

[英]Issue with custom capitalize String method, jQuery and Select2

我在使用jQuery和Select2时遇到了麻烦。

我为String原型创建了一个函数以将其大写。 另一方面,我使用的是jQuery 1.11.0和Select2 3.4.5,当我单击Select2时,出现了错误,因为在非字符串对象上调用了大写方法:

无法大写非字符串值。

 <div id="select2-drop" class="select2-drop select2-display-none select2-with-searchbox select2-drop-auto-width" style="left: 813.5px; width: 380px; top: 777px; bottom: auto;"> 

printStackTrace.implementation.prototype.createException @ http://my.domain.com/js/stacktrace.js:56 printStackTrace.implementation.prototype.run @ http://my.domain.com/js/stacktrace.js:43 printStackTrace @ http://my.domain.com/js/stacktrace.js:30 String.prototype.capitalize @ http://my.domain.com/js/orpheus.js:101 jQuery.access @ http:// my.domain.com/js/jquery.js:4122 jQuery.access @ http://my.domain.com/js/jquery.js:4094 .css @ http://my.domain.com/js/jquery .js:6911 AbstractSelect2 <.positionDropdown @ http://my.domain.com/js/select2/select2.js:1231 AbstractSelect2 <.opening @ http://my.domain.com/js/select2/select2.js :1322 SingleSelect2 <.opening @ http://my.domain.com/js/select2/select2.js:1840 AbstractSelect2 <.open @ http://my.domain.com/js/select2/select2.js:1265 SingleSelect2 <.initContainer / <@ http://my.domain.com/js/select2/select2.js:2039 AbstractSelect2 <.bind / <@ http://my.domain.com/js/select2/select2.js :651 jQuery.event.dispatch @ http://my.domain.com/js/jquery.js:4624 jQuery.event.add / elem Data.handle @ http://my.domain.com/js/jquery.js:4292

我用stacktrace.js库得到了stacktrace。 orpheus.js是我的JS文件,其中包含大写方法(针对测试进行了修改):

String.prototype.capitalize = function () {
    if( typeof this != "string" ) {
        console.log("Unable to capitalize non-string value.");
        console.log(this);
        console.log(printStackTrace().join("\n"));
        return this;
    }
    return this.charAt(0).toUpperCase() + this.slice(1).toLowerCase();
};

使用select2,我想将一个类添加到下拉列表中,但是我使用dropdownCss而不是dropdownCssClass来解决此问题。 但是真正的问题是,如果我使用字符串而不是对象/函数,为什么jQuery在非字符串对象上调用大写方法。 这真的很奇怪。

暂无
暂无

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

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