开发者

Use jquery :contains and and .eq() to modify text style within specific div

开发者 https://www.devze.com 2023-03-03 08:45 出处:网络
Is it possible to combine the use of :contains and .eq() to search for text within a specific div? I currently use the following two statements which create jquery statements based upon values grabbe

Is it possible to combine the use of :contains and .eq() to search for text within a specific div?

I currently use the following two statements which create jquery statements based upon values grabbed from the URL by PHP $_GET[].

The first simply checks for li开发者_高级运维nk text matching the current $platformString and turns it white.

$(\"a:contains('". $platformString . "')\").css(\"color\", \"#ffffff\");

The second shows and hides a div based upon a known value stored in section.

$(\".sidebar_content\").eq(" . $section .").toggle();

The problem is that there can be multiple matches of $platformString within other hidden divs, once these are matched they are also turned white.

Can I combine the two statements to simply highlight the $platformString within the current $section?

Many thanks for any advice you are able to provide.

Robert


Im not sure if i understand you correctly, but..

have you tried "a:visible:contains()" ?

This should only match the elements that are visible and contains the specified text.


This will search all the div's on the page for the text contained in platformString and wrap them in tags.

var platformString = "replaceMe";

$('div').html(function() {
   return $(this).html().replace(new RegExp(platformString, "ig"), '<strong>$&</strong>'));
});

It's similar to "highlighting" just replace <strong> with another kind of tag you wish to use.. maybe a <span> with a class for some custom effects?

EDIT: replace searchString with $& - matches the searched text and preserves capitalization

0

精彩评论

暂无评论...
验证码 换一张
取 消