使用“这个” jQuery 函数内部
我创建了一个 CalendarViewerPortlet 自定义对象 JS 对象。在此对象中,我存储诸如 portlet 的 id 及其上下文路径之类的内容。该对象还有许多自定义方法,有些用于获取/设置成员变量,有些用于执行特定操作。
当我尝试使用“this”引用对象的函数时。在 jQuery 函数内部,它会爆炸。我知道在这种情况下术语“this”可能指的是其他东西,但我不确定如何解决这个问题并让它引用该对象,正如我想要的那样。
这是有问题的代码:
jQuery.ajax({
url: jQuery(formSel).attr("action"),
type: "POST",
data: jQuery(formSel).serialize(),
beforeSend: function(xhr) {
jQuery(msgSel).hide();
jQuery(msgSel).html("");
jQuery(tableSel).hide();
jQuery(pagerSel).hide();
jQuery(cpSelector).block({
message: "<img src='"+this.getContextPath()+"/images/icon_loading.gif' align='absmiddle' alt='Loading...' /> Fetching events..."
});
},
注意“this.getContextPath()”。这就是代码失败的地方。我试图引用我的自定义对象的 getContextPath() 函数。我怎样才能做到这一点?
I have created a CalendarViewerPortlet custom object JS object. In this object, I am storing things like the portlet's id and its context path. The object also has many custom methods, some to get/set the member variables and some to do specific things.
When I try to reference a function of the object using "this." inside of a jQuery function, it blows up. I get that the term "this" in that context is probably referring to something else, but I am not sure how to get around the problem and have it reference the object, as I want it to.
Here is the offending code:
jQuery.ajax({
url: jQuery(formSel).attr("action"),
type: "POST",
data: jQuery(formSel).serialize(),
beforeSend: function(xhr) {
jQuery(msgSel).hide();
jQuery(msgSel).html("");
jQuery(tableSel).hide();
jQuery(pagerSel).hide();
jQuery(cpSelector).block({
message: "<img src='"+this.getContextPath()+"/images/icon_loading.gif' align='absmiddle' alt='Loading...' /> Fetching events..."
});
},
NOTE the "this.getContextPath()". That is where the code is failing. I am trying to reference the getContextPath() function of my custom object. How can I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
问题是 this.getContextPath() 代码稍后在作为 on 选项传递给 ajax() 函数的匿名函数中执行。因此,您想要的“this”(您的自定义 JS 对象)在方法执行时将不可用。您可以将其存储在变量中并让函数“闭包”作为引用。以下应该有效:
The problem is the code this.getContextPath() is executed later in an anonymous function passed as on option to ajax() function. So the 'this'(your custom JS object) you want will not be available at the point of the execution of the method. You can store it in a variable and let the function 'closure' the reference. The following should work:
也许尝试使用jQuery(this)
看来我误解了你。在给定的示例中,上下文可能会导致“this”引用 jQuery 对象。尝试以下技巧:
// 好吧,它确实引用了 jQuery 对象。这是因为您没有使用缩进来指出在 onBeforeSend 中调用代码:(
maybe try usingjQuery(this)
it seems I've misunderstood you. In the given example, the context may cause "this" refer to jQuery object. Try the following trick:
// well, it DOES refer jQuery object. it's because you don't use indentation to point that the code is called in onBeforeSend :(
您想要 which 对象的
getContextPath()
方法吗?快速的 Google 搜索仅显示它可用于 Java,而不是 Javascript。
You want the
getContextPath()
method from which object?A quick Google search only shows it to be available to Java, not Javascript.
您可以使用 在 javascript 中调用
例如:
在主体中,
只需在事件中调用 FirstFunc() ,它将使用您的自定义 this 执行 SecondFunc() 。
就你的情况而言
希望这有效
You can send your custom object as this in the other function by using call in javascript
eg:
in body
just call FirstFunc() in the event and it will execute the SecondFunc() with your custom this.
In your case
Hope this works
提示:您应该熟悉 Javascript 中的词法作用域
如果您在另一个对象中设置了 ajax,那么您可以尝试...
Tip: You should get familiarized with Lexical Scoping in Javascript
If you have the ajax setup inside another object then you could try...
这是一个绑定问题。当您在 jQuery 方法内调用此方法时,它通常会引用您正在执行任务的 DOM 或 HTML 元素。
我会推荐看看
可以解决您的问题。
This is a binding problem. When you call this inside a jQuery method it is usually referenced to the DOM- or HTML-Element you are performing your task on.
I would recomend a lok at
which solves your problems.