How to get a particular url using JQuery ..?
I wanted to identify each html page separately to add a开发者_运维问答ctive style like(.select). But I'm not sure how to get a page url in using JQuery..?
for example I want to give .select class to about.html page when someone going to click the about button.
Please help me, im looking forward to your valuable replies.
Thanks Paulson
The best solution would be to maintain a different css stylesheet for each page, but if you have to do it dynamically.
document.location.href
Is the standard way to ge the URL, and is not jQuery specific.
Do you have to use JavaScript to get the specific page? What I usually do is set a page variable on each page load. If you need to pass it through to the JavaScript/jQuery you cold then just create a new variable inside of a tag. Hope that helps!
You could try something like this:
var page = location.pathname;
$('a[href$=' + page + ']').addClass('select');
This gets the pathname portion of the URL, then uses the attribute ends with selector to find <a>
elements that that have an href
attribute that ends with the current path, and the .addClass()
method to add the select
class.
精彩评论