Jquery and Regex help needed
Here's HTML
<div id="taxonomylist">
<div class="item-list"><ul><li class="first"><a href="/?q=category/activity/energy" class="views-processed">Energy</a></li>
<li><a href="/?q=category/activity/energy/3-5" class="views-processed">3-5</a></li>
<li><a href="/?q=category/activity/energy/6-8" class="views-processed">6-8</a></li>
<li><a href="/?q=category/activity/energy/k-2" class="views-processed">K-2</a></li>
<li><a href="/?q=category/activity/forestry" class="views-processed">Forestry</a></li>
<li><a href="/?q=category/activity/forestry/3-5" class="views-processed">3-5</a></li>
<li><a href="/?q=category/activity/forestry/6-8" class="views-processed">6-8</a></li>
<li><a href="/?q=category/activity/forestry/k-2" class="views-processed">K-2</a></li>
<li><a href="/?q=category/activity/mining" class="views-processed">Mining</a></li>
<li><a href="/?q=category/activity/mining/3-5" class="views-processed">3-5</a></li>
<li><a href="/?q=category/activity/mining/6-8" class="views-processed">6-8</a></li>
<li class="last"><a href="/?q=category/activity/mining/k-2" class="views-processed">K-2</a></li>
</ul></div></div>
I need to hide all elements inside div except those that says THIS. I need to keep that one. So as for regex parsing - i need it to show only those values that: 1. Start with anything 2. then have structure: structure /activity/"anything here"/"nothing here" You can see what I mean by looking on the code above.
Flow of my logic 1. hide all li elements within particular div 2. if 'a' element inside li contains attribute href which is equal to /title/ then show this li element. My current code
$('li').css('display','none');
if ($('li').attr('href')=="*/activity/*/")
{
$('li').css('display','block');
}
if I do something like:
$('#div-id li').css('display','none');
then it does not wor开发者_StackOverflow中文版k. Im just tired, but need to finish work asap. help please.
Perhaps it solves your problem:
$("div#div-id li").each(function() {
var href = $("a", this).attr("href");
if ( href.match(/^.*\/activity\/.*\/$/i) ) {
alert($(this).text());
}
});
Check a demo at jsbin: http://jsbin.com/ozive4/2/edit
UPDATE: It was just a basic principle. Here is something more complete:
jQuery(function($) { function get() { $("div#div-id li").each(function() { var href = $("a", this).attr("href"); if ( href.match(/^.*\/activity\/.*\/$/i) ) { alert($(this).text()); } }); } $("button").click(function() { get(); }); });
And the demo: http://jsbin.com/ozive4/4/edit
UPDATE: This is the regex:
/^.\/activity\/.[a-z,A-Z]$/i
And the new demo: http://jsbin.com/ozive4/6/edit
I think you mean:
if ($('li').attr('href').match(/.*\/activity\/.*\/$/m)) {
Using James Padolsey's RegEx filter, I created this jsfiddle that hides the ones that don't match your pattern. Aside from the RegEx filter, the relevant code is
$('a:regex(href, .*/activity/.*/.*)').closest('li').hide();
EDIT: I changed to .hide(); from a .each() that set display:none. Must be getting late, not sure why I did it the long way originally :)
EDIT 2: Updated jsfiddle from version 7 to version 9.
for hiding all li except THIS you can use this code...
$('div.div-class ul li').hide();
$(' ul li').each(function(){
if($(this).text()=="THIS")
$(this).show();
});
DEMO
精彩评论