[英]how to make a script run every time i load data from WebMethod using ajax?
i have a problem that javascript script only loaded once when the page is loading,but i need it every time i get data from WebMethod on the server side 我有一个问题,即javascript脚本只在加载页面时加载一次,但每次我从服务器端的WebMethod获取数据时都需要它
here is my code: 这是我的代码:
My JavaSript ajax: 我的JavaSript ajax:
<script type="text/javascript">
$(document).ready(function () {
function InfiniteScroll() {
$('#divPostsLoader').html('<img src="img/loader.gif">');
$.ajax({
type: "POST",
url: "Home.aspx/GetLastArticles",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
if (data != "") {
$('#divPostsLoader:last').after(data.d);
}
$('#divPostsLoader').empty();
},
error: function (data) {
if (!document.getElementById('#divPostsLoader').innerHTML == "<p>END OF POSTS</p>")
$('#divPostsLoader').empty();
$('#divPostsLoader').html("<p>END OF POSTS</p>");
}
})
};
var b = false;
//When scroll down, the scroller is at the bottom with the function below and fire the lastPostFunc function
$(window).scroll(function () {
if ($(document).height() <= $(window).scrollTop() + $(window).height()) {
InfiniteScroll();
}
b = true;
});
if (!b) {
window.onload = function () {
InfiniteScroll();
};
}
});
</script>
and here is my javasript that i want it to fire everytime i get data from GetLastArticles
WebMethod: 这里是我的javasript,我希望每次从GetLastArticles
WebMethod获取数据时它都会触发:
<script type="text/javascript">
$(".tag_button").on('click', function (e) {
e.preventDefault(); // prevent default action - hash doesn't appear in url
$(this).parent().find('div').toggleClass('tags-active');
$(this).parent().toggleClass('child');
});
</script>
my aspx page (client side): 我的aspx页面(客户端):
<div class="tags_list">
<a class="tag-icon tag_button" href="#" >Tags</a>
<div class="tags">
<a class="tag-icon" href="#" >Tag1</a>
<a class="tag-icon" href="#">Tag2</a>
<a class="tag-icon" href="#">Tag3</a>
<a class="tag-icon" href="#">Tag4</a>
<a class="tag-icon" href="#">Tag5</a>
<a class="tag-icon" href="#">Tag6</a>
<a class="tag-icon" href="#">Tag7</a>
</div>
</div>
Change to the following and the eventhandler is attached to dynamically added elements too. 更改为以下内容,并且eventhandler也附加到动态添加的元素。
$(document).on('click', '.tag_button', function() {...});
Syntax explanation 语法解释
Just to clarify the code. 只是为了澄清代码。 The syntax is as follows: 语法如下:
$(document).on(events, cssSelector, handler);
See the full API on jQuery API documentation . 查看有关jQuery API文档的完整API 。 By selecting document
we make sure this event handler is loaded to all elements in the document now and in the future. 通过选择document
我们确保现在和将来将此事件处理程序加载到文档中的所有元素。
call your function in ajax success 在ajax成功中调用你的函数
success: function (data) {
if (data != "") {
$('#divPostsLoader:last').after(data.d);
}
$('#divPostsLoader').empty();
$(".tag_button").click();
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.