提问者:小点点

覆盖/替换jquery click函数


如果这是个显而易见的问题,请原谅我。

我正在处理一个无法删除或更改脚本的页面。 点击一个按钮,它将一些项目加载到带有ajax请求的页面上,然后为页面触发一个烦人的scrolljacking动画。

目标:我仍然希望项目加载时,按钮被点击,但我不希望滚动动画。

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

下面是一个简化的例子。

null

.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>

null

实际问题可以在这里找到:https://obsu.unionclouduat.org/groups我认为我的简化示例与此类似,但我不能百分之百确定。


共1个答案

匿名用户

您可以尝试unbind()并重新对事件?

// custom js, override must go here.

$(".load-more").unbind().click(function () {
        //loading another item
        $(".groups-listing").append('<div class="group"></div>');
});