Estoy tratando de volver a cargar un elemento en particular en el botón al hacer clic usando jQuery, pero cuando hago clic en el botón, la página completa se carga en este elemento. Probé el siguiente código que carga toda la página en este elemento. Quiero cargar solo un elemento en particular.
<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>Tienes que cargar la página usando $.get() y extraer el fragmento #demo :
$("#button").click(function() { $.get(location.href).then(function(page) { $("#demo").html($(page).find("#demo").html()) }) })Puede usar Ajax, luego crear un elemento con responseText, luego ponerlo en el div:
<head> <script> $(document).ready(function() { $("#button").click(function(){ $.ajax({ url: location.href, type: 'GET', sucess: function(data) { refreshedPage = $(data); newDemo = refreshedPage.find("#demo").html(); $('#demo').html(newDemo) } }); } } </script> </head> <body> <div id="example"> <p id="demo">hi</p> <button id="button" type="button">press</button> </div> </body>Pero cargas toda la página, puede que no me sea útil. Sugiero hacer un script php que solo devuelva los datos solicitados y llamarlo a través de Ajax al cargar la página y al hacer clic en el botón ...