Uso jquery para desplazarme automáticamente en la publicación del blog. Normalmente funcionan bien, pero no se desplaza ni funciona en absoluto cuando cargo esa página a través de AJAX. El problema podría ser cómo estoy llamando a ajax para cargar la página. ¿Será un problema de función de devolución de llamada que no estoy entendiendo bien? aquí está el código ajax que estoy usando:
function loadme() { var xhttp; if (window.XMLHttpRequest) { xhttp = new XMLHttpRequest(); } else { xhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById("loadcontent").innerHTML = this.responseText; } }; xhttp.open("GET", "http://xxxyyy.com/blogs/", true); xhttp.send(); }Todos funcionan, pero el desplazamiento automático de jquery post no funcionará. ¿Se debe a la función de devolución de llamada? No estoy seguro... Alguien sugiere o corrige el código... Agradecería la ayuda voluntaria
Además , hice una función de devolución de llamada alternativa, pero eso tampoco funciona ...
<div id="loadcontent"> Content to load/replace</div> <button onclick="loadDoc('http://xxxyyy.com/blogs', myFunction)">Browse Blogs</button> //ajax with callback function function loadDoc(url, cFunction) { var xhttp; xhttp=new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { cFunction(this); } }; xhttp.open("GET", url, true); xhttp.send(); } function myFunction(xhttp) { document.getElementById("loadcontent").innerHTML = xhttp.responseText; }Dado que ha etiquetado jquery y también mencionó jquery en su respuesta, estoy proporcionando una solución jquery.
//bind click event to the button, set an id for the button to make it just for that particular button $(button).click(function() { ajaxRequest("url", loadcontent); }); // this will be the function for ajax, with the callback as parameter function ajaxRequest(url, callback) { $.ajax({ url: url, method: "get", success: function (response) { callback(response); }, error: function (jqXHR, exception) { // handle errors } }); } // this will be passed as callback to the ajaxRequest function //you just need to set the innerHTML and the use animate to scroll to the bottom or to whatever height you would like function loadcontent(message) { $("#loadcontent").html(message); $("#loadcontent").animate ({ scrollTop: $("#container").prop("scrollHeight") }, 10); }