使用“这个” jQuery 函数内部

发布于 2024-09-12 05:26:37 字数 786 浏览 4 评论 0原文

我创建了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

好菇凉咱不稀罕他 2024-09-19 05:26:37

问题是 this.getContextPath() 代码稍后在作为 on 选项传递给 ajax() 函数的匿名函数中执行。因此,您想要的“this”(您的自定义 JS 对象)在方法执行时将不可用。您可以将其存储在变量中并让函数“闭包”作为引用。以下应该有效:

var calendarViewer = 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='"+calendarViewer.getContextPath()+"/images/icon_loading.gif' align='absmiddle' alt='Loading...' /> Fetching events..."
  });
},

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:

var calendarViewer = 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='"+calendarViewer.getContextPath()+"/images/icon_loading.gif' align='absmiddle' alt='Loading...' /> Fetching events..."
  });
},
陌上青苔 2024-09-19 05:26:37

也许尝试使用jQuery(this)

看来我误解了你。在给定的示例中,上下文可能会导致“this”引用 jQuery 对象。尝试以下技巧:

function myCode() {
 var th = this;
 $.ajax( ... , th.getContextPath() ... );
}

// 好吧,它确实引用了 jQuery 对象。这是因为您没有使用缩进来指出在 onBeforeSend 中调用代码:(

maybe try using jQuery(this)

it seems I've misunderstood you. In the given example, the context may cause "this" refer to jQuery object. Try the following trick:

function myCode() {
 var th = this;
 $.ajax( ... , th.getContextPath() ... );
}

// well, it DOES refer jQuery object. it's because you don't use indentation to point that the code is called in onBeforeSend :(

只等公子 2024-09-19 05:26:37

您想要 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.

等你爱我 2024-09-19 05:26:37

您可以使用 在 javascript 中调用
例如:

function SecondFunc() {
    alert(this.MyVar);
}
function FirstFunc() {
    var obj = {MyVar:"Test"}
    SecondFunc.call(obj);
}

在主体中,

<input type="button" onclick="FirstFunc()" value="test" />

只需在事件中调用 FirstFunc() ,它将使用您的自定义 this 执行 SecondFunc() 。

就你的情况而言

function raiseSendRequest()
{
sendRequest.call(YourCustomObject);
}

function sendRequest()
{

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..."
  });
}

希望这有效

You can send your custom object as this in the other function by using call in javascript
eg:

function SecondFunc() {
    alert(this.MyVar);
}
function FirstFunc() {
    var obj = {MyVar:"Test"}
    SecondFunc.call(obj);
}

in body

<input type="button" onclick="FirstFunc()" value="test" />

just call FirstFunc() in the event and it will execute the SecondFunc() with your custom this.

In your case

function raiseSendRequest()
{
sendRequest.call(YourCustomObject);
}

function sendRequest()
{

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..."
  });
}

Hope this works

吃不饱 2024-09-19 05:26:37

提示:您应该熟悉 Javascript 中的词法作用域

如果您在另一个对象中设置了 ajax,那么您可以尝试...

{
  //code
  var ObjectSelf = this;

  //refer to your object via ObjectSelf

}

Tip: You should get familiarized with Lexical Scoping in Javascript

If you have the ajax setup inside another object then you could try...

{
  //code
  var ObjectSelf = this;

  //refer to your object via ObjectSelf

}
不爱素颜 2024-09-19 05:26:37

这是一个绑定问题。当您在 jQuery 方法内调用此方法时,它通常会引用您正在执行任务的 DOM 或 HTML 元素。

我会推荐看看

jQuery.proxy()

可以解决您的问题。

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

jQuery.proxy()

which solves your problems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文