使用jQuery重新加载特定元素

Aru*_*esh 4 javascript ajax jquery

我正在尝试使用jQuery重新加载按钮上的特定元素,但是当我单击按钮时,整个页面将加载到此元素中.我尝试了以下代码,在此元素中加载整个页面.我想只加载特定元素.

<script>
    $(document).ready(function() {
        $("#button").click(function() {
            $("#demo").load(location.href + "#demo");
        });
    });
</script>
</head>

<body>
    <div id="example">
        <p id="demo">hi</p>
        <button id="button" type="button">press</button>
    </div>
Run Code Online (Sandbox Code Playgroud)

Tus*_*har 13

您可以使用load()选择器.

$("#demo").load(location.href + " #demo"); // Add space between URL and selector.
                                 ^
Run Code Online (Sandbox Code Playgroud)

这将仅加载URL中的#demo元素location.href.