Javascript jQuery-创建一个函数并调用它
如何在jQuery中创建函数并调用它 这似乎不正确:Javascript jQuery-创建一个函数并调用它,javascript,jquery,Javascript,Jquery,如何在jQuery中创建函数并调用它 这似乎不正确: var scrollLink = function() { $('html, body').animate({ scrollTop: $( $(this).attr('href') ).offset().top-20 }, 300); }; $('.js-ask').click(function(e) { e.preventDefault();
var scrollLink = function() {
$('html, body').animate({
scrollTop: $( $(this).attr('href') ).offset().top-20
}, 300);
};
$('.js-ask').click(function(e) {
e.preventDefault();
$('[href="' + this.dataset.target + '"]').tab('show');
reportReadMessages();
});
$(".js-scroll").click(function(e, scrollLink) {
e.preventDefault();
scrollLink();
});
您没有在
scrollLink()
中传递此的内容。它是一个参数为空的函数,当它不理解这是什么时。因此,请将其更改为:
$('.js-ask').click(function(e) {
e.preventDefault();
$('[href="' + this.dataset.target + '"]').tab('show');
reportReadMessages();
});
$(".js-scroll").click(function(e) {
e.preventDefault();
$('html, body').animate({
scrollTop: $( $(this).attr('href') ).offset().top-20
}, 300);
});
您没有以正确的方式传递滚动链接。这就是它不起作用的原因
也可以通过以下方式扩展该功能:
$.fn.scrollLink = function() {
$('html, body').animate({
scrollTop: $( $(this).attr('href') ).offset().top-20
}, 300);
};
您可以调用以下元素:
$(this).scrollLink();
$(selector).scrollLink();
有关更多信息,请参阅。您没有在滚动链接()中传递这个的内容。它是一个参数为空的函数,当它不理解这是什么时。因此,请将其更改为:
$('.js-ask').click(function(e) {
e.preventDefault();
$('[href="' + this.dataset.target + '"]').tab('show');
reportReadMessages();
});
$(".js-scroll").click(function(e) {
e.preventDefault();
$('html, body').animate({
scrollTop: $( $(this).attr('href') ).offset().top-20
}, 300);
});
您没有以正确的方式传递滚动链接。这就是它不起作用的原因
也可以通过以下方式扩展该功能:
$.fn.scrollLink = function() {
$('html, body').animate({
scrollTop: $( $(this).attr('href') ).offset().top-20
}, 300);
};
您可以调用以下元素:
$(this).scrollLink();
$(selector).scrollLink();
有关更多信息,请参阅。Nice!非常感谢。是的,对不起,我只是从jQuery开始。但你的解决方案让我明白了很多事情。谢谢,很好!非常感谢。是的,对不起,我只是从jQuery开始。但你的解决方案让我明白了很多事情。谢谢