0

このようなページがあります。

<!DOCTYPE html>
<html>
<head>
  <title>JQM</title>
  <meta charset=utf-8 />
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://code.jquery.com/mobile/latest/jquery.mobile.css"> 
  <script src="http://code.jquery.com/jquery-1.8.1.min.js"></script>
  <script src="http://code.jquery.com/mobile/latest/jquery.mobile.js"></script>
  <script>
    $(function(){
      $('[data-role="list-divider"]').toggle(function(){
        $('.'+$(this).attr('data-link')).addClass('show');
        $(this).children().removeClass('ui-icon-plus').addClass('ui-icon-minus');

      },function(){
        $('.'+$(this).attr('data-link')).removeClass('show');
        $(this).children().removeClass('ui-icon-minus').addClass('ui-icon-plus');
      });
    });
  </script>
</head>
<body>

<div data-role="page">
    <div data-role="header">

    </div>
    <div data-role="content">   


    </div>
</div>

</body>
</html>

コンテンツ領域のサーバーから動的にhtmlを追加しています。問題は、コンテンツを動的に追加すると、ページ上で静的に作成したjquery関数が機能しないことです。

<script>
        $(function(){
          $('[data-role="list-divider"]').toggle(function(){
            $('.'+$(this).attr('data-link')).addClass('show');
            $(this).children().removeClass('ui-icon-plus').addClass('ui-icon-minus');

          },function(){
            $('.'+$(this).attr('data-link')).removeClass('show');
            $(this).children().removeClass('ui-icon-minus').addClass('ui-icon-plus');
          });
        });
      </script>

HTMLを静的に追加すると、すべてのコードが正常に機能し、すべてが正常に機能します。私の質問は、HTMLがサーバーからページに追加されたら、このjqueryを実行できるようにするにはどうすればよいですか?

私はこれを実行し、それは機能しましたが、.onを使用するよりエレガントな方法はありますか、それともこれで問題ありませんか?

//got html blob
  deferred.success(function (res) {

   $(function () {
                $('[data-role="list-divider"]').toggle(function () {
                    $('.' + $(this).attr('data-link')).addClass('show');
                    $(this).children().removeClass('ui-icon-plus').addClass('ui-icon-minus');

                }, function () {
                    $('.' + $(this).attr('data-link')).removeClass('show');
                    $(this).children().removeClass('ui-icon-minus').addClass('ui-icon-plus');
                });
            });
 });
4

5 に答える 5

0

コードを見ると、コンテンツを動的にロードしている場所がわかりません。ただし、そのスクリプトを実行する場合。そのスクリプトの前でdocument.ready()を試す必要があります

于 2012-11-01T18:53:46.350 に答える
0

作成されていない要素にイベントハンドラーをバインドすることはできません。これをチェックしてください:

http://api.jquery.com/on/

于 2012-11-01T18:54:49.530 に答える
0

.On()ページに動的に追加されたコンテンツにハンドラーをアタッチするには、jqueryメソッドを使用する必要があります。これを確認してください-.on

これがうまくいくかもしれないようなもの(テストしなかった):-

  $(function(){
      $('[data-role="list-divider"]').on('toggle' ,function(event){
        $('.'+$(this).attr('data-link')).addClass('show');
        $(this).children().removeClass('ui-icon-plus').addClass('ui-icon-minus');

      },function(event){
        $('.'+$(this).attr('data-link')).removeClass('show');
        $(this).children().removeClass('ui-icon-minus').addClass('ui-icon-plus');
      });
    });
于 2012-11-01T18:54:54.570 に答える
0

jqueryは現在、ドキュメントの準備ができてコンテンツがまだロードされていないときに実行されるため、要素を検出してイベントを接続することはできません。スクリプトをに追加するとajax.success、問題が解決するはずです。

于 2012-11-01T18:59:44.320 に答える
0

これを試して:

    $(function(){
      $(document).on('toggle', '[data-role="list-divider"]', function(){
        $('.'+$(this).attr('data-link')).addClass('show');
        $(this).children().removeClass('ui-icon-plus').addClass('ui-icon-minus');

      },function(){
        $('.'+$(this).attr('data-link')).removeClass('show');
        $(this).children().removeClass('ui-icon-minus').addClass('ui-icon-plus');
      });
    });

しかし、それをテストしていません。on('click')の場合、これは通常機能します。

'toggle'トリガーをドキュメント全体($(document))にバインドしてから、トリガーされた要素を確認します。このようにして、DOMの初期化後に作成された要素を検出できます。

于 2012-11-01T19:00:22.287 に答える