javascript - function to define current class in js -
What I want to do is a function that defines a "current" class for "code"; Li & gt; The tag depends on the page name. For example, if the name of the page is index.php, then I have to decide whether I want the class to be "on" to and
& lt; Ul id = "navigation" & gt; & Lt; Li & gt; & Lt; A href = "index.php" & gt; & Lt; / Li & gt; & Lt; / Ul & gt;
& lt; Li & gt;
tag, if the link is similar to the page name, is there any way to do this? Or am I on a waste mission?
I think what you are asking, do you want to change this to the current page of the link Pointing here what the code looks like.
var list = document.getElementsByTagName ('a'); Var page = window.location.pathname; Var i = list.length; While (i -) {if (list [i] .src.indexOf (page)> gt; {list [i] .className = 'current'; }}
Note that this is not a very accurate method, the basic structure is correct, but for example a link somewebsite.com is actually pointing to somewebsite.com/index.php is. Therefore, it can cause a problem on the home page based on the link. Also, how it determines how your links are set up, you probably have to go to the purse page variable. This will return something like /help/faq/foo.php while the page can only have a link to faq / foo.php. It all depends on the setup of your site, so I will leave it for you to tweak for .
I added more details since the posting, so I thought I would need to make you a list & lt; Li & gt; >> All of the links in the tag
in the page & lt; A & gt;
is not a tag.
Comments
Post a Comment