简体   繁体   English

offcanvas jquery 触发器在 React 中不起作用

[英]offcanvas jquery trigger not working in React

I have included jasny's bootstrap offcanvas menu in a react app.我在React应用程序中包含了jasny 的 bootstrap offcanvas 菜单。 While I can trigger and hide the open menu through console in jasny's example like so:虽然我可以在jasny 的示例中通过控制台触发和隐藏打开的菜单,如下所示

$('.navmenu').offcanvas('hide')

But doing the same in my react app is throwing error: Uncaught TypeError: $(...).offcanvas is not a function(…)但是在我的反应应用程序中做同样的事情会抛出错误:Uncaught TypeError: $(...).offcanvas is not a function(...)

I am guessing its got something to do with javascript closure.我猜它与 javascript 闭包有关。 Is there any other way to trigger this?有没有其他方法可以触发这个?

I ended up having to trigger like this on my page load:我最终不得不在我的页面加载时像这样触发:

      if(window.innerWidth > 1000)
          document.getElementById('navbar-toggle').click();

How I solved it:我是如何解决的:

// to show it
let myOffcanvas = new window.bootstrap.Offcanvas(document.getElementById('offcanvasId'))
myOffcanvas.show();

and then然后

// to hide it
let myOffcanvas = window.bootstrap.Offcanvas.getInstance(document.getElementById('offcanvasId'))
myOffcanvas.hide();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM