React调用bootstrap函数
React call bootstrap function?
由于某些原因,我不能使用react-bootstrap。所以我想调用bootstrap的函数,如'modal',但它似乎不工作,我得到了这个错误:
modal不是函数
这是我在componentDidMount()中尝试的:
$('#my-modal').modal();
或
ReactDOM.findDOMNode(this.refs.myModal).modal();
或
$(this.refs.myModal).modal();
我的模态:
<div class="modal fade" id="my-modal" ref="myModal">
...
</div>
我想知道是否有解决这个问题的方法,或者我必须使用一些库,如'react-bootstrap' ?
谢谢你!
为了确保在调用"modal"函数之前加载bootstrap脚本,我添加了一个按钮:
<button type="button" class="btn btn-default" data-toggle="modal" data-target="#my-modal">Open Modal</button>
当这个按钮被点击时,模态打开。现在,我用onSubmit函数在模态中创建了一个表单
<div class="modal fade" id="my-modal" ref="myModal">
<form onSubmit={this.testModal}>...</form>
</div>
testModal() {
// here i tried to call modal('hide') like i did above, but still get the error "modal is not a function"
}
我知道如何解决这个问题了。首先,我需要使jQuery全局(我不知道为什么$不起作用,或者可能是冲突)
import jQuery from 'jquery';
window.jQuery = jQuery;
然后,我需要在我的js文件中要求引导:
require('bootstrap')
// or just require('bootstrap/js/modal'); require('bootstrap/js/transition');
而不是在index.html中包含此脚本:
<script src="/node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
现在,我可以调用
jQuery('#my-modal').modal();
UPDATE:我发现可能有一个问题,如果你不添加jQuery.noConflict(true)
在你的构造函数
相关文章:
- Bootstrap-“;未捕获的类型错误:undefined不是函数;
- HTML5(Bootstrap)通过函数调用运行动画
- 从 Bootstrap Typeahead 中的数据属性中调用函数
- Bootstrap/JQuery-单选按钮组事件函数
- 将函数绑定到Twitter Bootstrap Modal Ajax Complete
- Twitter Bootstrap Popover添加了一个带有事件的回调函数
- 如何在Bootstrap 2.3.2中弹出dropup之前调用函数
- Bootstrap.dropdown()“;未捕获的类型错误:undefined不是函数;
- JQUERY hover() 函数不适用于其子函数,使用 bootstrap 框架
- Bootstrap Modal JS Uncatch TypeError: undefined 不是函数
- 复选框 Javascript 函数在 Bootstrap modal 中不起作用
- Meteor 和 Bootstrap 表,如何调用 detailFormatter 函数
- AngularJS UI Bootstrap 模态无法从作用域执行函数
- Angularjs twitter bootstrap.错误:element.focus() 不是一个函数
- 与 Bootstrap 按钮关联的 jQuery 函数仅执行一次
- Twitter bootstrap typeahead 自定义按键 ENTER 函数
- 从HTML页面调用Bootstrap函数
- 不止一个下拉菜单扰乱了jQuery/Bootstrap函数
- 为什么我可以't打开BootStrap模式,同时执行jQuery回调函数
- 在除法上调用JavaScript函数(bootstrap模式弹出)