react实现HTML文档搜索

1  搜索按钮

       <div style={{marginBottom: 10}}>
              <Form layout='inline' ref={this.formRef} name="search">
                    <Row gutter={24}>
                                    <Form.Item name="search"
                                    >
                                        {getFieldDecorator("search")(<Input  />)}
                                    </Form.Item>
                                    <Form.Item name="search"
                                    >
                                        <Input style={{width:'70px'}} disabled={true}
                                            value={searchInputValue}
                                        />
                                    </Form.Item>

                                    <Button type="primary" icon='search' onClick={this.onTextSearch}>搜索</Button>&emsp;
                                 
                                </Row>
                 </Form>
          </div>

2  CSS样式

.result{
    background: #77ff45;
}
.res
{
    color: #fbff61;
    background: #ff013e;
}

3  搜索方法

    onTextSearch = () => {
        $(".result").removeClass("res");//去除原本的res样式
        let searchText = this.props.form.getFieldsValue()
        var key = searchText.search; //取key值
        if (!key) {//key为空
            $(".result").removeClass("result");//去除原本的res样式
            this.state.oldKey = "";
            this.setState({
                searchInputValue: '',
            });
            return; //key为空则退出
        }
        if (this.state.oldKey != key) {
            $(".result").removeClass("result");//去除原本的res样式
            let pos = new Array();
            var regExp = new RegExp(key+'(?!([^<]+)?>)', 'ig');//正则表达式匹配
            // 高亮操作
            $("#filelog-container div")[0].innerHTML = $("#filelog-container div")[0].innerHTML.replace(regExp, "<span   class=\"result\" >" + key + "</span>");
            $(".result").each(function () {//获取所有匹配值位置
                pos.push(this.offsetTop);
            });
            this.state.oldKey = key;
            this.state.pos = pos;
            this.state.index = 0;
            this.state.oldCount = $(".result").length;

        }else {
            if(this.state.oldCount!=0){
                this.state.index++;
                if(this.state.index==this.state.oldCount){
                    this.state.index = 0 ;
                }
            }
        }
        this.setState({
            searchInputValue: this.state.oldCount==0?this.state.index+"/"+this.state.oldCount:this.state.index+1+"/"+this.state.oldCount,
        });
        $(".result:eq(" + this.state.index + ")").addClass("res");//当前位置关键词改为红色字体
        $("#filelog-container").scrollTop( this.state.pos[ this.state.index] - 200);//跳转到指定位置
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值