jQuery and the this object
jQuery and the this object
jQuery太强大了:(
)String.prototype.trim = function () {
return this.replace(/^('s| |'u00A0)+|('s| |'u00A0)+$/g, "");
}
当我尝试添加上述代码时,我得到"this。" Replace不是函数"。
我意识到jQuery引用自己为this
,那么你打算如何引用this
呢?
您发布的代码在String上定义了一个"trim"方法。它允许你这样做:
" some random string ".trim();
听起来像是你复制并粘贴了trim函数的BODY到其他jQuery函数中,像这样:
$('#myfield').change(function () {
this = this.replace(/^('s| |'u00A0)+|('s| |'u00A0)+$/g, "");
});
试试这个:
$('#myfield').change(function () {
$(this).val($(this).val().trim());
});
问题解决:
我忘了在函数末尾加上分号它会进入jQuery函数
String.prototype.trim = function () {
return this.replace(/^('s| |'u00A0)+|('s| |'u00A0)+$/g, "");
};
(function ($) {
......
相关文章:
- Object.prototype using 'this'
- 成员函数中的Javascript“this”object==“window”
- Javascript Object - using jQuery and this
- $this->Js->object() 根据值自动对其内容进行排序
- 在Javascript中,在IE8中,附加到事件中的Object方法将“窗口对象”而不是对象本身保留在“this”
- jQuery change html in $(this) object
- window.alert(this) 应该始终是 object window
- JavaScript - object "this"
- $(this).parent().attr('id') 获取 '[object window]&
- “this”是getMediaUserCallback中的窗口,而不是Object
- Javascript static/singelton - this vs _this vs object name
- 怎么可能object==这个,但是object.member!=this.member在Javascript中
- 为什么“this”不等于“Object”,为什么属性是“undefined”
- Javascript文本对象符号This vs Object Name
- '的语义是什么;this.object.position'在Three.JS's轨迹球
- jQuery中This Object的Target子元素
- 将' this '的继承扩展到' object '的方法/属性
- Using `this` vs Object.create(prototype)
- javascript object this
- Javascript's Object(this)在多边形中的用法