1

我想在一个 div 中有一个链接列表,将内容加载到第二个 div 中。我已经阅读了我能找到的关于这件事的所有内容……但它似乎不起作用。这是我所拥有的:

<div id="scentwrap" align="center">
        <div id="sounds"><h2>Sounds like you need to celebrate!</h2><div>
        <div id="myList">
            <div id="listtit">
                <br>Try more recipes below!
            </div>
            <div id="list">
                <ul>
                    <li> <a href="scent2.html">Soothing Blend</a></li> <li>item # </li> <li>item # </li> <li>item # </li> <li>item # </li> <li>item # </li> <li>item # </li>
                    <li>item # </li> <li>item # </li> <li>item # </li> <li>item # </li> <li>item # </li> <li>item # </li> 
                    <li>item # </li> <li>item # </li> <li>item # </li> <li>item # </li> <li>item # </li> <li>item # </li> 
                    <li>item # </li> <li>item # </li> <li>item # </li> <li>item # </li> <li>item # </li> <li>item # </li> 
                </ul>
            </div>
        </div>
        <div id="currentscent">
            <div id="recipe"> 

                <h4>- Celebration Blend -</h4>
                <ul>
                    <li>Diced Ginger </li>
                    <li>Grapefruit Peels </li>
                    <li>Frankincese Oil </li>       
                </ul>
                <p>
                 Lorem ipsum lorem dadsum doodsum lercem vi can ojr <br> ig thn sas vbg 
                 Lorem ipsum lorem dadsum doodsum<br> lercem vi can ojr ig thn sas vbg 
                </p>
            </div>
            <div id="rec">
                <img src="recimg.jpg"/>
            </div>
        </div>
    </div>

Javascript:

$(function() {
$('a[href=scent2.html]').click(function() {
    $('#recipe').load('scent2.html');
   return false;
});

非常感谢任何帮助!

4

2 回答 2

1

您没有关闭 jQuery 文档就绪事件。代码应如下所示:

   $(function() {
     $('a[href="scent2.html"]').click(function() {
       $('#recipe').load('scent2.html'); 
       return false;
     });
   });
于 2013-04-02T18:59:58.100 回答
0

尝试检查加载文件是否出错

<script>
$("#success").load("/not-here.php", function(response, status, xhr) {
  if (status == "error") {
    var msg = "Sorry but there was an error: ";
    $("#error").html(msg + xhr.status + " " + xhr.statusText);
  }
});
  </script>

编辑

确保您的呼叫符合同源政策。来自 Jquery 文档网站

Due to browser security restrictions, most "Ajax" requests are subject to the same origin policy; the request can not successfully retrieve data from a different domain, subdomain, or protocol.

file:// protocol因此,如果您的网站正在使用httporhttps等​​加载,则尝试加载 using将引发错误。

于 2013-04-02T18:53:08.360 回答