gpt4 book ai didi

javascript - onclick() 和 .on ('click' ,function()) 之间的区别?

转载 作者:行者123 更新时间:2023-11-28 15:19:07 25 4
gpt4 key购买 nike

<分区>

$(document).ready(function(){
// Add smooth scrolling
$('.button').children().onclick(function(event) {

// Make sure this.hash has a value before overriding default behavior
if (this.hash !== "") {
// Prevent default anchor click behavior
event.preventDefault();

// Store hash
var hash = this.hash;

// Using jQuery's animate() method to add smooth page scroll

$('html, body').animate({
scrollTop: $(hash).offset().top
}, 1000, function(){
// Add hash (#) to URL when done scrolling (default click behavior)
window.location.hash = hash;

});
} // End if
});
});




$(document).ready(function(){
// Add smooth scrolling to all links
$('.button').children().on('click', function(event) {

// Make sure this.hash has a value before overriding default behavior
if (this.hash !== "") {
// Prevent default anchor click behavior
event.preventDefault();

// Store hash
var hash = this.hash;

// Using jQuery's animate() method to add smooth page scroll
// The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
$('html, body').animate({
scrollTop: $(hash).offset().top
}, 1000, function(){

// Add hash (#) to URL when done scrolling (default click behavior)
window.location.hash = hash;
});
} // End if
});
});

当我使用 onclick() 函数时,单击按钮时没有滚动效果;它只跳转到没有任何滚动效果的文章。

但是当我使用 on('click',function()) 时有滚动效果。

这两者有什么区别?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com