Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark.js实现关键词高亮 #55

Open
alanhe421 opened this issue Jun 20, 2018 · 1 comment
Open

Mark.js实现关键词高亮 #55

alanhe421 opened this issue Jun 20, 2018 · 1 comment

Comments

@alanhe421
Copy link
Owner

轮子:https://markjs.io/

alanhe421 pushed a commit that referenced this issue Jun 20, 2018
@alanhe421
Copy link
Owner Author

alanhe421 commented Jun 20, 2018

  1. index.html资源引入
    <script src="https://cdn.bootcss.com/mark.js/8.11.1/mark.min.js"></script>
  1. 声明
declare const Mark: any;
  1. 检索高亮
declare const Mark: any;
...
export class HomeComponent implements OnInit {

    keyword$ = new Subject<string>();

    constructor() {
        this.keyword$.debounceTime(400).subscribe(keyword => {
            this.performMark(keyword);
        });
    }
    performMark(keyword: string) {
        const instance = new Mark('.home-content');
        instance.unmark();
        if (keyword) {
            instance.mark(keyword);
        }
    }
<input type="text" class="form-control" placeholder="关键词检索" #keyword
                                (keyup)="keyword$.next(keyword.value)">
<div class="home-content">...</div>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant