I have the following scenario:
I have 5 possible values on a page:
[value 1]
...
[value 5]
What I want to do is grab each value and brackets...
Example Content:
<p>velit, sed quia non numquam eius modi tempora incidunt ut labore et
dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam,
quis nostrum exercitationem ullam [value 1] corporis suscipit laboriosam, nisi
ut aliquid ex ea commodi consequatur? Quis autem vel eum iure
reprehenderit qui in ea voluptate</p>
... and move to another part of the page, let's say 开发者_如何学运维in the header.
How hard is this?
If you don't need to know where it came from, you can use $('body').text() to get all the text off the website, and then regexp to find the elements.
var text = $('body').text();
var m = new Regexp('\[([^\]])+\]').exec();
$.each(m, function(index, value){
//add value to header
}
Btw, this code is not tested, and on large sites this could take long. Try to narrow the scope as much as possible(by passing another element instead of body)
You could get the html contents, and do a regex on that string, or since you only have 5 possibilities, you could just do it the slower easier jQuery way:
var possibilities = ['[value 1]', '[value 2]', '[value 3]', '[value 4]', '[value 5]'];
for(var i in possibilities){
if ( $(":contains('" + possibilities[i] + "')").length ) {
alert(possibilities[i] + ' was found');
}
}
This might be the most simple solution to follow, depending on how comfortable you are with different parts of jQuery and regex.
精彩评论