简体   繁体   English

Wicket-IE8-当通过Ajax动态添加标签时,将执行Javascript事件侦听器

[英]Wicket - IE8 - Javascript event listeners arent executed, when tags are dynamically added over ajax

I have spent several hours, maybe days stucked on a very weird problem :( 我花了几个小时,也许几天都停留在一个很奇怪的问题上:(

I am creating an application that is based on the Wicket solution. 我正在创建基于Wicket解决方案的应用程序。 It works perfectly in IE9,IE10, Chrome and FF. 它可以在IE9,IE10,Chrome和FF中完美运行。 Strange is, that i have tested it in IE8 too and it works in 99% of cases (IE instances on different computers + totally identical version of IE8) too. 奇怪的是,我也在IE8中对其进行了测试,并且它也可以在99%的情况下(不同计算机上的IE实例+完全相同的IE8版本)工作。 But now the PROBLEM. 但是现在有问题了。

PROBLEM: I am creating dynamic content over AjaxLink button. 问题:我正在通过AjaxLink按钮创建动态内容 After clicking the button the WebMarkupContainer model is changed and WebMarkupContainer is refreshed (based on Ajax, so the page isnt reloaded complete, but only the container is). 单击按钮后,将更改WebMarkupContainer模型并刷新WebMarkupContainer(基于Ajax,因此不会完全重新加载页面,但只有容器是)。

Every item in the container has added AjaxFormComponentUpdatingBehavior. 容器中的每个项目都添加了AjaxFormComponentUpdatingBehavior。 In onComponentTag method, i add tag.put("onchange","some jsFunctionCalling....");. 在onComponentTag方法中,我添加了tag.put(“ onchange”,“ some jsFunctionCalling ....”);。 The problem is, that after clicking on the item, no event is invoked. 问题是,单击该项目后,没有任何事件被调用。 I have tried add the onchange listener over .add(new AttributeModifier.....), but the result is still same. 我试过在.add(new AttributeModifier .....)上添加onchange侦听器,但结果仍然相同。 As i have said, i tried the same code in the same version of IE on another PC and it works perfectly. 就像我说的那样,我在另一台PC上的相同版本的IE中尝试了相同的代码,并且运行完美。 Interesting is, that after refreh of the page everything work perfect, until new item to WebMarkupContainer is added. 有趣的是,刷新页面后,一切都可以正常运行,直到添加到WebMarkupContainer的新项目为止。 After that no item listeners work until the page is refreshed again. 此后,直到再次刷新页面之前,项目侦听器均不起作用。

One of the latest idea, that i got is, that problem isn't in the code, but in the settings of IE (maybe security). 我得到的最新想法之一是,该问题不在代码中,而在IE的设置中(也许是安全性)。 Have anybody any idea? 有人知道吗? What setting could be set different and cause these problems? 哪些设置可能会有所不同并导致这些问题? Is there any settings on Wicket site, that can solved this? Wicket网站上是否有任何设置可以解决此问题? Is there some setting that can blocked registration of these listeners to DOM, if they are added dynamically over ajax? 如果通过ajax动态添加这些监听器,是否有某些设置可以阻止这些监听器向DOM注册?

I didn't tried it but IMHO there are three options you can try: 我没有尝试过,但是恕我直言,您可以尝试三种选择:

  1. Instead of adding "onchange" by yourself, add OnChangeAjaxBehavior and make all work in wicket. 不必自己添加“ onchange”,而是添加OnChangeAjaxBehavior并使所有工作都在wicket中进行。 Downside is server roundtrip on every event. 缺点是每次事件都发生服务器往返。
  2. Add data-attributes ( AttributeModifier.append("data-param1", "foobar") ) to push your parameters into html and call ajaxRequestTarget.appendJavaScript("attachOnChangeHandler()"); 添加数据属性( AttributeModifier.append("data-param1", "foobar") )以将参数推送到html中,并调用ajaxRequestTarget.appendJavaScript("attachOnChangeHandler()"); after the click event on the AjaxLink. 在AjaxLink上的click事件之后。 attachOnChangeHandler() should be your js function to add onchange handler to every item which needs it. attachOnChangeHandler()应该是您的js函数,用于向需要它的每个项目添加onchange处理程序。 And over data-attributes you can access your parameters. 通过数据属性,您可以访问参数。
  3. Since Wicket 6 : To avoid mixing to much js with Wicket, you could subscribe to one of the global AJAX events. 从Wicket 6开始 :为了避免与Wicket混用太多js,您可以订阅全局AJAX事件之一。 The solution in your case would be almost the same as in 2. Just add a listener in js for "/ajax/call/success" (see if the call relates to your component by checking the id) and add the onchange handler there. 您所用的解决方案与2中的解决方案几乎相同。只需在js中为“ / ajax / call / success”添加一个侦听器(通过检查id来查看该调用是否与您的组件相关),然后在其中添加onchange处理程序。 This is IMHO the best solution without mixing custom js with Wicket. 这是恕我直言,最好的解决方案,而无需将定制js与Wicket混合使用。

The solution provided by @peterchon (attaching event handlers higher in the DOM than the elements which are going to be replaced by wicket) would work in every other case, but you have "onchange" which applies only to input, textarea and select elements. @peterchon提供的解决方案(在DOM中将事件处理程序附加到比将要由wicket替换的元素更高的事件处理程序)在所有其他情况下都可以使用,但是您拥有“ onchange”,它仅适用于input,textarea和select元素。

BTW the page is "working" after refresh, since the whole page is rendered and browser can properly attach the handlers. 顺便说一句,刷新后页面将“工作”,因为整个页面已呈现,浏览器可以正确附加处理程序。

You can try this method: 您可以尝试以下方法:

/* this will catpure the target that triggered the event */
function getEventTarget( e ) {
  e = e || window.event;
  return e.target || e.srcElement;
}

function doSomething( e ) {
  var that = getEventTarget( e );
  if( that.tagName.toLowerCase() === 'a' ) { // specify the target, in this cas <a>
    // Do something
  }
}

parentElement.onclick = doSomething;

This script basically will capture any event, then will pass the variable of target to the function that will perform something. 该脚本基本上将捕获任何事件,然后将target的变量传递给将执行某些操作的函数。

Hopefully this will work for you. 希望这对您有用。

You try to achieve something using a non-wicket JavaScript/Ajax way. 您尝试使用非标准的JavaScript / Ajax方法实现某些目标。 This is fine, but also makes it very messy. 很好,但是也很混乱。

Please check this fine article about passing parameters from JavaScript to wicket and vice versa. 请查看这篇有关将参数从JavaScript传递到wicket以及反之亦然的文章。 I think it will suit your needs. 我认为它将满足您的需求。

http://wickedsource.org/2013/01/07/rolling-your-own-ajax-behavior-with-wicket/ http://wickedsource.org/2013/01/07/rolling-your-own-ajax-behavior-with-wicket/

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

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