覆盖替换一个jquery点击函数。

问题描述 投票:0回答:1

如果这是一个明显的问题,请原谅我。

我在一个页面上使用了一个我无法删除或更改的脚本。点击一个按钮,通过ajax请求将一些项目加载到页面上,然后触发一个恼人的页面滚动劫持动画。

我们的目标是。 我仍然希望当按钮被点击时加载项目, 但我不希望有滚动动画。

我能够在页面中添加我自己的自定义js(下面是添加默认脚本的地方)。我如何中断或覆盖原始脚本来删除滚动行为?

这里是一个简化的例子。

.group{
	background: cyan;
	height:400px;
	width:400px;
	margin:10px
}
.load-more{
	background:#88f;
	width:360px;
	text-align:center;
	margin:10px;
	padding:20px;
  cursor:pointer;
}
<!-- markup -->
<div class="groups-listing">
	<div class="group"></div>
</div>
<div class="load-more">Load more</div>

<!-- scripts -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
	$(".load-more").click(function () {
		//loading another item
		$(".groups-listing").append('<div class="group"></div>');
		
		//annoying animated scrolling
		$("html, body").animate(
			{
				scrollTop: $(".groups-listing .group").last().offset().top
			},2000
		);
	});
</script>
<script>
  // custom js, override must go here.
</script>

实际问题可以在这里找到。https:/obsu.unionclouduat.orggroups。我认为我的简化例子是类似的,但我不是100%确定。

html jquery jquery-animate
1个回答
1
投票

你可以尝试 unbind() 并重新签署事件?

// custom js, override must go here.

$(".load-more").unbind().click(function () {
        //loading another item
        $(".groups-listing").append('<div class="group"></div>');
});
© www.soinside.com 2019 - 2024. All rights reserved.