jQuery将方法添加到对象,然后触发事件就可以了

时间:2016-03-22 20:22:20

标签: jquery dom javascript-events dynamic-function

大家晚上好,在这里我再次提出一个非常具体的问题我无法找到答案。 考虑以下情况: 我有一个jQuery对象,3/22/2016 2:50:51 PM - Error - (19696, 16664) - The remote server returned an error: (404) Not Found.: \r\n\r\n at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult) at Microsoft.TeamFoundation.Release.Data.WebRequest.PlatformHttpClient.EndGetResponse(IAsyncResult asyncResult) at Microsoft.TeamFoundation.Release.Data.WebRequest.RestClientResponseRetriever.EndGetAsyncMemoryStreamFromResponse(IAsyncResult asyncResult, IPlatformHttpClient PlatformHttpClient) at Microsoft.TeamFoundation.Release.Data.WebRequest.RestClientResponseRetriever.EndDownloadString(IAsyncResult asyncResult, IPlatformHttpClient PlatformHttpClient) at Microsoft.TeamFoundation.Release.Data.WebRequest.RestClient.EndPost(IAsyncResult asyncResult) at Microsoft.TeamFoundation.Release.Data.Proxy.RestProxy.HttpRequestor.<>c__DisplayClass0_0.<GetPostCaller>b__0(String url, String body) at Microsoft.TeamFoundation.Release.Data.Proxy.RestProxy.BaseOrchestratorServiceProxy.InitiateRelease(String releaseTemplateName, IDictionary`2 deploymentPropertyBag) at Microsoft.VisualStudio.Release.ViewModel.ViewModels.PipelineV2.ReleaseCreatorViewModelExtension.InvokeReleaseAction(ReleaseCreatorViewModel viewModel, Func`3 releaseAction, Func`2 releaseViewModelCreator, Func`3 longProcessCreator, Action`1 showViewAction, Func`2 loadReleases, Func`5 doesBuildExists) at Microsoft.VisualStudio.Release.ViewModel.ViewModels.PipelineV2.ReleaseCreatorViewModel.<get_CreateAndStartCommand>b__120_0(Object param) at Microsoft.VisualStudio.Release.ViewModel.Helpers.RelayCommandV2.Execute(Object parameter) at MS.Internal.Commands.CommandHelpers.CriticalExecuteCommandSource(ICommandSource commandSource, Boolean userInitiated) at System.Windows.Controls.Primitives.ButtonBase.OnClick() at System.Windows.Controls.Button.OnClick() at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e) at System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e) at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget) at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target) at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent) at System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e) at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget) at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target) at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args) at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args) at System.Windows.Input.InputManager.ProcessStagingArea() at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input) at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport) at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel) at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler) 。这发生在init调用中,例如$wrapper = $(this);。正如您已经可以想象的那样$('#unique-wrapper-id').initWrapper()'initWrapper()中的一个函数。

现在,仍然在jQuery.fn来电中我向initWrapper()这样添加了一个功能

$wrapper

然后,在单击包装器中的元素时执行的回调中,我调用$wrapper.getValue = function() { /* typical wrapper function */}。 在另一端,我会听到常规的jQuery更改事件,而这里不再适用。

$wrapper.trigger('change')

好的,所以jQuery更改事件进程中的某个地方$('#unique-wrapper-id').change(function() { var $wrapper = $(this); $wrapper.getValue(); // $wrapper.getValue() is not a function }); 会丢失。没问题,我只是在使用

的init调用中将它附加到DOMElement本身

getValue()

这可以按预期工作,我可以在侦听端的jQuery对象后面的DOMElement上执行$wrapper[0].getValue = function { /* typical wrapper function */ }方法。

然而,有两件事让我感到困惑:

  • 为什么jQuery对象上的getValue()会在更改事件过程中丢失?
  • 为什么jQuery构造函数(getValue())不将DOMElement的var $wrapper = $(this);函数复制到jQuery对象中?

先谢谢,如果有要求,我会尝试制作一个jsfiddle(或者更容易用jQuery,我只是不能让jsfiddle按预期工作-.-)

1 个答案:

答案 0 :(得分:1)

如果$wrapper是相同的元素,您应该能够将getValue存储在.data()

var getValue = function() { /* typical wrapper function */};
$wrapper.data("getValue", getValue);
$("#unique-wrapper-id").change(function() {
    var $wrapper = $(this);
    $wrapper.data().getValue();
});