1

I am using the following way in order to insert an external html page into my div:

      $.ajax({
          url: "myHTML.html",
          method: 'GET',
          success: function (data) {
              $("#outputDiv").html(data);
          },
          Error: function (err) {
              alert("ERROR: " + err);
          }
      });

which works fine, the issue is that I would like to run some JavaScript functions that are located in the "myHTML.html" file, Is there a way to do something like that?

Thanks (=

4

1 に答える 1

0

多くの場合、コンテンツをロードするページにスクリプトを保持する方がはるかに簡単です。これは、すべて同じajxを使用してロードし、すべて同じ関数を呼び出すファイルのグループがある場合に特に当てはまります。これにより、コードリビジョンで複数のファイルを変更する必要がなくなります。

$.ajax({
      url: "myHTML.html",
      method: 'GET',
      success: function (data) {
          $("#outputDiv").html(data);
          /* new html exits so can run code here that affects it*/
          newHtmlAdded();
      },
      error: function (err) {/* note typo on capital "E" fixed*/
          alert("ERROR: " + err);
      }
  });

  function newHtmlAdded(){
       fixmFormStyle();
       addMyValidation(); 
      doSomethingElse();
  }

readyそれ以外の場合は、ロードされているページでイベントがすでに発生していることを認識することが重要です。これは、readyハンドラーでラップされたコードがすぐに起動することを意味します。コードがリモートファイルのhtmlの前にある場合、対応するhtml要素がDOMに存在する前に起動し、何もしません。

リモートファイルのhtmlの後に配置されたコードは機能します。これは、それが参照するhtmlがすでに処理されているためです。

于 2012-06-20T02:40:00.567 に答える