Abe*_*ler 1 javascript jquery this
免责声明:我问的是具体用途this,而不是this一般用途.所以,请不要谷歌/复制/粘贴速度答案(:
我有以下的javascript/jquery代码:
var req = {};
function getData()
{
var fromPage = 0;
var toPage = 1;
req = $.ajax({
url: "/_vti_bin/lists.asmx",
type: "POST",
dataType: "xml",
data: xmlData,
complete: onSuccess,
error: function (xhr, ajaxOptions, thrownError) {
alert("error: " + xhr.statusText);
alert(thrownError);
},
contentType: "text/xml; charset=\"utf-8\""
});
req.fromPage = fromPage;
req.toPage = toPage;
}
function onSuccess(resp) {
alert(this.fromPage);
}
Run Code Online (Sandbox Code Playgroud)
我觉得这个代码fromPage在两个地方使用时很困惑(抱歉不是我的代码).
是否this指的是变种的内声明的getData或一个是金req的对象?或者完全是其他的......
任何帮助赞赏.
根据jQuery.ajax文件:
所有回调中的this引用是在设置中传递给$ .ajax的context选项中的对象; 如果未指定context,则这是对Ajax设置本身的引用.
换句话说,由于您没有设置context选项,因此this将选项对象{...}作为参数传递给$.ajax.
你发布的代码似乎错了:它fromPage从错误的对象中读取.如果您设置fromPage选项对象,它将起作用:
req = $.ajax({
//other options here...
fromPage: fromPage
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
175 次 |
| 最近记录: |