SelectBoxIt允许您用华丽且功能丰富的下拉菜单替换丑陋且难以使用的 HTML 选择框。
我实现了 SelectBoxIt 和 <select>
在我的里面render(return())
方法无法识别它。
我在网上搜了一下,几乎只有一个source on Github我不明白它的建议,你可以在这一点上帮助我。
简而言之,我的结论是 SelectBoxIt 自然不支持 ReactJS。
因此我们需要一个解决方案或技巧。
下面是来自他们网站的示例代码,并在我的项目中完全使用了它:
<!doctype html>
<html>
<head>
<meta charset=utf-8>
<title>SelectBoxIt demo</title>
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/css/bootstrap-combined.min.css" />
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/themes/base/jquery-ui.css" />
<link type="text/css" rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<link rel="stylesheet" href="http://gregfranko.com/jquery.selectBoxIt.js/css/jquery.selectBoxIt.css" />
</head>
<body>
<select name="test">
<option value="SelectBoxIt is:">SelectBoxIt is:</option>
<option value="a jQuery Plugin">a jQuery Plugin</option>
<option value="a Select Box Replacement">a Select Box Replacement</option>
<option value="a Stateful UI Widget">a Stateful UI Widget</option>
</select>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js"></script>
<script src="http://gregfranko.com/jquery.selectBoxIt.js/js/jquery.selectBoxIt.min.js"></script>
<script>
$(function() {
var selectBox = $("select").selectBoxIt();
});
</script>
</body>
</html>
以下select
没有受到影响:
<select
id="sel1"
// ref={s => { this.selectBox = s; }}
onChange={ event => this.onSelectChange(event.target.value) }
>
{options}
</select>
<select name="test">
放入后有效 render(return())
但是<select id="sel1" >
里面render(return())
尚未更改为 SelectBoxIt 形状。请注意,我评论了 ref={s => { this.selectBox = s; }}
只是为了防止显示错误。
最佳答案
由于这是一个 jquery 插件,因此您需要确保安装它:
npm install -S jquery
然后您需要将其导入到您的脚本中:
import * as $ from 'jquery';
现在,在您的 React 组件中,在 componentDidMount 中应用 jquery 初始化。唯一的问题是您需要使用 ref 来查找节点。
render() {
return <select ref={s => { this.selectBox = s; }} />
}
componentDidMount() {
$(this.selectBox).selectBoxIt();
}
一些引用here .
关于javascript - 需要 SelectBoxIt 和 ReactJS 支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47070080/