Why is .on() Not working as expected in jquery?

user2622662

I've just started learning jQuery. And below is the code that i tried for attaching the events "mouseover" and "mouseout" for the div elements added dynamically. But when I tried running this in browser, i got no results. I dont know what is goin wrong... I hit a search for the use and syntax of using .on() of jquery and no favourable results.. Can anyone tell me, how to make my below code work?..

    <html>
       <head>
          <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
          <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
          <script type="text/javascript">
             $(function(){
                 $("#divid").on("mouseover",".test", function(){
                     $(this).css("background-color", "blue");
                 }).on("mouseout",".test", function(){
                     $(this).css("background-color", "white");
                });
             });

             function AddBox(){
                var div = $("<div></div>").addClass("test").text("Another box");
                $("#divTestArea1").append(div);
             }
          </script>
      </head>
      <body>
         <div id="divTestArea1">
            <a href="javascript:void(0);" onclick="AddBox();">Add box</a>
            <div class="test" id="divid">This is a colored box</div>
         </div>
      </body>
   </html>

Thanks in Advance...;-)

bipen

problem here,

you are adding dynamic div to #divTestArea1 here $("#divTestArea1").append(div);

but you are delegating your event to #divid so this search for div class test inside <div id=divid> which is not there.

try this

 $("#divTestArea1").on("mouseover",".test", function(){
   ....
  }

with this, you are delegating the mouseover event of .test div to#divTestArea1 ..which is what we need.. and should work...

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Why "this" in a jQuery plugin is not working as expected?

分類Dev

jQuery .when().then() not working as expected

分類Dev

Ternary Operator in jQuery not working as expected

分類Dev

Could someone explain why my Adjacent Selector isn't working as expected in jQuery?

分類Dev

Why is KNN algorithm in scikit not working as expected?

分類Dev

Why is accepts_nested_attributes_for not working as expected?

分類Dev

why sorting du -h not working as expected?

分類Dev

jQuery removing dom elements not working as expected

分類Dev

jquery launch checkbox check not working as expected

分類Dev

Why is JQuery Code not working on Server?

分類Dev

Why is my Realm Kotlin Generics workaround not working as expected?

分類Dev

Why is a WHERE clause to exclude rows in SQLite not working as expected?

分類Dev

Why are mathematical operations and output of environment variables in an ELSE branch not working as expected?

分類Dev

jQuery nested each loop not working as expected using Trello API

分類Dev

AJAX call is not working as expected

分類Dev

.vimrc file not working as expected

分類Dev

*ngIf not working as expected with observable

分類Dev

ItemIsAutoTristate flag not working as expected

分類Dev

KeyboardAvoidingView not working as expected on IOS

分類Dev

PowerShell variables not working as expected

分類Dev

XPath logical 'and' not working as expected?

分類Dev

CancellationTokenSource not working as expected on a TaskCompletionSource

分類Dev

string formatting not working as expected

分類Dev

Tuples in Scala not working as expected

分類Dev

strpos() not working as expected

分類Dev

PHP Dateformat not working as expected

分類Dev

Update not working as expected

分類Dev

ifelse not working as expected in R

分類Dev

$("#form").submit(); is not working as expected

Related 関連記事

  1. 1

    Why "this" in a jQuery plugin is not working as expected?

  2. 2

    jQuery .when().then() not working as expected

  3. 3

    Ternary Operator in jQuery not working as expected

  4. 4

    Could someone explain why my Adjacent Selector isn't working as expected in jQuery?

  5. 5

    Why is KNN algorithm in scikit not working as expected?

  6. 6

    Why is accepts_nested_attributes_for not working as expected?

  7. 7

    why sorting du -h not working as expected?

  8. 8

    jQuery removing dom elements not working as expected

  9. 9

    jquery launch checkbox check not working as expected

  10. 10

    Why is JQuery Code not working on Server?

  11. 11

    Why is my Realm Kotlin Generics workaround not working as expected?

  12. 12

    Why is a WHERE clause to exclude rows in SQLite not working as expected?

  13. 13

    Why are mathematical operations and output of environment variables in an ELSE branch not working as expected?

  14. 14

    jQuery nested each loop not working as expected using Trello API

  15. 15

    AJAX call is not working as expected

  16. 16

    .vimrc file not working as expected

  17. 17

    *ngIf not working as expected with observable

  18. 18

    ItemIsAutoTristate flag not working as expected

  19. 19

    KeyboardAvoidingView not working as expected on IOS

  20. 20

    PowerShell variables not working as expected

  21. 21

    XPath logical 'and' not working as expected?

  22. 22

    CancellationTokenSource not working as expected on a TaskCompletionSource

  23. 23

    string formatting not working as expected

  24. 24

    Tuples in Scala not working as expected

  25. 25

    strpos() not working as expected

  26. 26

    PHP Dateformat not working as expected

  27. 27

    Update not working as expected

  28. 28

    ifelse not working as expected in R

  29. 29

    $("#form").submit(); is not working as expected

ホットタグ

アーカイブ