我正在尝试使用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>
解决方法
您可以将
load()
与选择器一起使用.
$("#demo").load(location.href + " #demo"); // Add space between URL and selector. ^
这将仅加载location.href URL中的#demo元素.