简体   繁体   English

ASP.Net Ajax PageMethod-保留对DOM对象的引用

[英]ASP.Net Ajax PageMethod - retain reference to DOM object

When calling an ASP.Net PageMethod, we call it as follows: 当调用ASP.Net PageMethod时,我们将其调用如下:

function doSomething(htmlElement)
{    
     PageMethods.GetText(onSuccess, onFailure);
}

What is the best way to retain a reference to the htmlElement in the above example, so that we may continue to work with it in the onsuccess method? 在上面的示例中,保留对htmlElement的引用的最佳方法是什么,以便我们可以在onsuccess方法中继续使用它?

Thanks for any help in advance 感谢您的任何帮助

Due to the fact that Javascript supports closures , you won't need to worry about maintaining the reference to the element; 由于Javascript支持闭包 ,因此您无需担心维护对元素的引用。 since it's lexically scoped within onSuccess(assuming you're inlining an unnamed function in the place of onSuccess.) 因为它在onSuccess内是词法范围的(假设您在onSuccess的位置内联了一个未命名的函数。)

Simply put, the function you put in for onSuccess can already use the reference to the element as if it were passed in as a parameter. 简而言之,为onSuccess放入的函数已经可以使用对元素的引用,就好像该元素作为参数传递一样。

function doSomething(htmlElement)
{         
    PageMethods.GetText(function(x, y){ var v = htmlElement /*won't be null*/   } , onFailure);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM