1

私はinitial.html別の html ページからロードしようとしています:

$("#hidden_demo_div").load("modules/another.html", function (data) {
    var value = $(data).filter("#demo_div");
    var html = value.html(); // THERE IS NO <strong>Hello</strong> here!!!
}

ここにanother.htmlからのスニペット

<html>
    .........................
    <head>
    <script type="text/javascript">
        $(document).ready(function() {
             anotherMethodInvocation();
        });
    </script>
    </head>

    <div id="demo_div"></div>
</html>

次はanother.htmlの JS で私は持っています:

function anotherMethodInvocation() {    
    $("#demo_div").append("<strong>Hello</strong>");
}

問題は、(読み込み時のコールバック関数で) 静的な HTML だけを取得し、変更しないのはなぜですか?

更新 1:

a) 「#hidden_​​demo_div」はinitial.html (.load を含む JS コードがリンクされている HTML) にあります。この要素は BODY で宣言されています。

b)BODYに(1つのhtmlファイルに)入れても機能しません

<div id="hidden_demo_div"></div>

そして(別のhtmlファイルへ)

<div id="demo_div"></div> 

で。

4

1 に答える 1

0

リモート ページをロードするときに、readyイベントがメイン ページで既に発生していることを認識しておくことが重要です。これは$(document).ready(function() {}、リモート ページにラップされたコードが即座に実行されることを意味します。

リモート ファイル内のコードが参照先の html よりも前にある場合、その html はコードの起動時に存在しないため、検索されません。

リモートページでこの構造を試してください:

<div id="demo_div"></div>
<!--  "demo_div" now exists , can run code on it -->
<script type="text/javascript">            
       anotherMethodInvocation();

</script>
于 2012-11-16T12:29:19.257 に答える