Bootstrap 4 activate.bs.scrollspy事件未触发
我正在使用Bootstrap v4.0.0
我已经包含了必要的JavaScript文件(jQuery,popper和Bootstrap)以及必要的CSS文件。
这是HTML:
Content. More content. $(function() { // As per the official docs: // https://getbootstrap.com/docs/4.0/components/scrollspy/#events $('[data-spy="scroll"]').on('activate.bs.scrollspy', function() { console.log("This event is not firing..."); }); });
滚动时菜单项会正确突出显示,但JavaScript事件activate.bs.scrollspy
未触发。
我也尝试将事件挂钩到navbar
本身,但它也不会触发:
$('#my-navbar-collapse').on('activate.bs.scrollspy', function () { console.log("Not firing either..."); })
我曾经在Bootstrap 3中使用这个代码并且工作得很好。
有什么想法吗?
谢谢。
由于某些原因在这里解释 ,当您使用body
作为spy元素时,事件会在window
上激活。
$(window).on('activate.bs.scrollspy', function () { console.log("This event is not firing..."); });
演示: https : //www.codeply.com/go/aN4tfy0zU0
编辑
目标元素可以在第二个事件参数中获得:
$(window).on('activate.bs.scrollspy', function (e,obj) { console.log(obj.relatedTarget); });