点击时无法加载

Lef*_*nos 3 jquery load onclick

我试图让下面的脚本运行,但我不能,任何建议?关键是我试图从其他页面调用外部内容.当我单独编写它是可以的,但是当我尝试从列表中加载它或通过单击时,它不能按预期工作

<div id="test">
    <a href="1/1.html">text 1</a>
    <a href="1/2.html">text 1</a>
</div>  

<b>see the text:</b>
<ol id="new-nav"></ol>

<script>
    $("#test").click(function () {
        $("#new-nav").load("href");
    });
</script> 
Run Code Online (Sandbox Code Playgroud)

Adi*_*dil 12

Instead of passing "href" to load这不是一个有效的资源pass the href of link being clicked,我也改变了选择器绑定与锚点击.放入$(document).ready将确保dom Element在使用前的可用性,使用它是安全的.

$(document).ready(function(){         

    $("#test a").click(function (e) {

        // Stop the link from changing the page
        //e.preventDefault();

        // Your jQuery code.

        $("#new-nav").load($(this).attr("href"));
    });

});
Run Code Online (Sandbox Code Playgroud)

  • 您还应该阻止锚链接的默认操作.`event.preventDefault()` (3认同)