English 中文(简体)
$(this).load(function () { .... } ); not working along with JSTestDriver
原标题:

The source code adds a load event handler to an element using JQuery. I am testing this using JSTestDriver.

The code looks something like this:-

       $(this).load(function () {
           alert("Foo");
       });

When I run the tests the alert("Foo") never happens. If I change it to:-

       $(this).ready(function () {
           alert("Foo with Ready");
       });

The test works.

If however, the source code is executed separately within an HTML, then it runs fine and I can get the alert("Foo") even when the event is "load".

Is this a bug in JSTestDriver or am I doing something incorrectly? Do, I have to wait till all the event handlers get triggered in the unit tests. (Something on the lines of Thread.join()) ?

-Ajay

问题回答

not sure why the load event is not being binded. an even better question is why jquery has two different methods with the same name (load() to bind an event, and load() to load some ajaxed content). here is an alternative syntax however that you migth want to try.

$(this).bind( load , handler);

if this works for you, please select the check mark next to this answer. i am trying to get enough rep to leave comments haha. thanks!

also, where is your binding code? is it possible that you are binding the event after the image has already loaded?

I believe your problem is not in the event binding, but in the meaning of $(this) in your code.

Could we get a broader scope?

One way to test would be to replace the $(this) with the actual selector for the element you are trying to trigger the event with.

or even right before the event binding, make this call to make sure the id of the element matches the id of the intended element.

alert($(this).attr( id ));




相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签