我有一个div,其中我有一些争论。我需要从该div中搜索文本并突出显示该单词(使用css类)。接下来它搜索下一个单词出现和上一次出现。 我可以在jquery中作为小提琴中的例子 http://jsfiddle.net/3MVNj/5/ 你能告诉我如何在棱角分明的js中达到这个目的吗? plunker http://plnkr.co/edit/V805fEjJAUbAZIuafJcW?p=preview
<!DOCTYPE html>
<html ng-app="app">
<head>
<link data-require="bootstrap-css@3.x" data-semver="3.2.0" rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" />
<script data-require="angular.js@*" data-semver="1.3.0-beta.5" src="https://code.angularjs.org/1.3.0-beta.5/angular.js"></script>
<script data-require="ui-bootstrap@0.10.0" data-semver="0.10.0" src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.10.0.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<style>
.highlightedText {
background: yellow;
}
</style>
<body>
<div ng-controller="contr">
<input type="text" ng-model="searchtext" />
<button ng-click="searchFirst()">FirstText</button>
<button ng-click="nextSearch()">NextSearch</button>
<button ng-click="preSearch()">PreviousSearch</button>
<div id="contend">hello I need to search text from the contend
.On click of search it search the first word.
</div>
</div>
<script>
var app=angular.module('app',[]);
app.controller("contr",function($scope){
$scope.searchFirst=function(){
alert('search First text')
}
$scope.nextSearch=function(){
alert('nextSearch')
}
$scope.preSearch=function(){
alert('preSearch')
}
});
</script>
</body>
</html>
有没有办法搜索过滤器?
答案 0 :(得分:0)
您可以使用旧的搜索算法,例如:http://plnkr.co/edit/As9iyCVtMMNH7lyprJA9?p=preview
添加的是
<button ng-click="searchFirst(searchtext)">FirstText</button>
示例搜索,仅转发(prev实现而非AngularJS相关)(请参阅运行示例的链接):
尝试搜索&#34; sear&#34; ...仅对该字词进行了测试... =)
app.controller("contr",function($scope){
var searcher = new Searcher();
var div = angular.element(document.getElementById('content'));
$scope.searchFirst=function(str){
searcher.setOffset(0);
searcher.setContent(div.text());
div.html(searcher.search(str));
}
$scope.nextSearch=function(){
div.html(searcher.search());
}
$scope.preSearch=function(){
// you implement
}
});