2

.load() を使用してメニューをロードすると、うまく機能します。しかし、このプリロードされたメニューから同じ方法でサブメニューをロードできないようです。

ここに私のメニューがあります:

<!doctype html>
<html lang="fr">
<head>
  <meta charset="utf-8">
  <title>Titre de la page</title>
  <link rel="stylesheet" href="style.css">
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  <script type="text/javascript" src="loader.js"></script>
</head>
<body>
<div id="wrapper">
    <div id="menu">
        <img src="images/logo.jpg" /><br>
        Illustrator &amp; Art Director
        <p><br><br><br>
        <a href="#" id="about">About Ātman</a><br>
        <a href="#" id="illustration">Illustration</a><br>
        <a href="#" id="graphic">Graphic Art</a><br>
        <a href="#" id="contact">Contact</a></p>
    </div>
    <div id="main"></div>
    <div id="boutons"></div>
</div>
</body>

loader.js は次のとおりです。

$(document).ready(function(){
    // load index page when the page loads
    $("#main").load("illustration.html #aacc");
    $("#boutons").load("boutons.html #illustrations");
    $("#about").click(function(){
        $("#main").load("about.html");
    });
    $("#illustration").click(function(){
        $("#main").load("illustration.html #aacc");
        $("#boutons").load("boutons.html #illustrations");
    });
    $("#graphic").click(function(){
        $("#main").load("graphic.html #navet");
        $("#boutons").load("boutons.html #graphic");
    });     
    $("#contact").click(function(){
        $("#main").load("contact.html");
    });
$("#img1").click(function(){
    $("#main").load("illustration.html #aacc");
});
$("#img2").click(function(){
    $("#main").load("illustration.html #navet");
});
    $("#lol").click(function(){
        alert('you clicked me!');
    });
});

メニューからロードされたサブメニュー (boutons.html) は次のとおりです。

<body>
    <div id="illustrations">
        <table border="0" width="200" cellpadding="1" cellspacing="1">
            <tr>
                <td><a href="#" id="img1"><img src="images/bouton/BI1.jpg" alt="" /></a></td>
                <td><a href="#" id="img2"><img src="images/bouton/BI5.jpg" alt="" /></a></td>
                <td><a href="#" id="lol">About Ātman</a></td>
            </tr>
        </table>
    </div>
</body>

illustration.html から読み込みたいコンテンツは次のとおりです。

<body>
    <div id="aacc">
        <img src="images/I/AACC.jpg" alt="AACC" />
        <div id="en">NO ADVERTISING EXCEPT BETC   poster for open door day (concept, art direction, paper sculpture)</div>
        <div id="fr">PAS DE PUB SAUF BETC   affiche pour la journée porte ouverte (concept, direction artistique, sculpture en papier)</div>
    </div>
    <div id="navet">
        <img src="images/I/navet.jpg" alt="navet" />
        <div id="en">NAVET  illustration for comics cover (paper sculpture)</div>
        <div id="fr">NAVET  illustration pour couverture de bande dessinée (sculpture en papier)</div>
    </div>  
</body>

テスト用に「笑」を作ったのですが、うまくいきません。何も起こりません。

よろしくお願いします:)

4

2 に答える 2

2

ノードがDOMにロードされた、イベントをバインドする必要があります。例:

それ以外の

$(document).ready(function(){
     $("#boutons").load("boutons.html #illustrations");
     //...

     //#illustrations is not loaded yet, $('#img1') and $('#img2') are empty
     $("#img1").click(function(){
         $("#main").load("illustration.html #aacc");
     });
     $("#img2").click(function(){
         $("#main").load("illustration.html #navet");
     });
})

あなたはできる :

$(document).ready(function(){
     $("#boutons").load("boutons.html #illustrations",
         //this callback is called *after* .load() has completed
         function(){
             $("#img1").click(function(){
                 $("#main").load("illustration.html #aacc");
             });
             $("#img2").click(function(){
                 $("#main").load("illustration.html #navet");
             });
         }
     );

もう1つの方法(よりクリーンなIMHO)は、最初からここにあり、DOMを離れることのないノードにイベントを委任することです。

$(document).ready(function(){
    // this basically says "if any child of '#boutons' matches the '#img1'
    // selector, execute this click handler"
    $('#boutons').on( 'click', '#img1', function(){
        //inside this handler, 'this' refers to the '#img1' item
        $("#main").load("illustration.html #aacc");
    });
    $('#boutons').on( 'click', '#img2', function(){
        $("#main").load("illustration.html #navet");
    });

    //...
    $("#boutons").load("boutons.html #illustrations");
});

on関数の詳細については、 jQueryのドキュメントを確認してください。

于 2013-02-21T08:50:47.973 に答える
0

イベント委任があなたが探しているものだと思います。

JQuery のバージョンによっては、 の代わりにliveorを使用することもできます。onclick

于 2013-02-21T08:55:01.263 に答える