我正在通过peepcode jQuery示例(https://peepcode.com/products/jquery),我选择更新所有依赖关系。JQuery.each将字符串文字转换为字符串。为什么?
不足为奇的是,我遇到了一个问题,关于字符串和String对象之间的JavaScript(大讨论在这里:Why are there two kinds of JavaScript strings?)的区别
一个问题我打的是jQuery.each字符串文字转换为字符串jQueryUI方法removeClass无法正确处理的对象。这是修复问题的代码片段。
我想知道如果错误是在jQuery.each错误地将字符串文字转换为字符串对象或应该removeClass假设它可以得到一个字符串文字或字符串对象。
更新:我刚刚意识到这是另一种解决办法: Strings in array are no longer strings after jQuery.each()
下面的代码:
jQuery.fn.extend({
taskStates:["task-empty", "task-x", "task-apostrophe", "task-dash"],
resetTaskStateClassNames: function() {
var elements = this;
// Commented code breaks as "this" becomes the String object not the literal
// and removeClass does not check for String Object
// jQuery.each(jQuery.fn.taskStates, function(){
// elements.removeClass(this); // this is the taskState
// })
jQuery.fn.taskStates.forEach(function(ts) {
elements.removeClass(ts);
});
return this;
},