How to get href value property for anchor tag in jquery?

If I have the following tag. What is the best JQuery methodto extract the value for "page" from the href.

Bạn đang xem: How to get href value property for anchor tag in jquery?

2Malcolm


*

The first thing that comes to my mind is a one-liner regex:

var pageNum = $("#specificLink").attr("href").match(/page=(<0-9>+)/)<1>;

*

*

I see two options here

var link = $("a").attr("href");var equalPosition = link.indexOf("="); //Get the position of "="var number = link.substring(equalPosition + 1); //Split the string & get the number.I dont know if you"re gonna use it for paging and have the text in the -tag as you have it, but if you should you can also do

var number = $("a").text();

*

First of all you need to lớn extract the path with something lượt thích this:

$("a#myLink").attr("href");Then take a look at this plugin:http://plugins.jquery.com/project/query-object

It will help you handle all kinds of querystring things you want to lớn do.

/Peter F


*

Here"s a method that works by transforming the querystring into JSON...

Xem thêm: Bí Quyết Trắng Da Của Ngọc Trinh, Học Để Có Làn Da Trắng Hồng

var liên kết = $("a").attr("href");if (link.indexOf("?") != -1) var query = link.split("?")<1>; eval("query = " + query.replace(/&/ig, "",").replace(/=/ig, ":"") + "";"); if (query.page) alert(unescape(query.page)); else alert("No page parameter"); else alert("No querystring");I"d go with a library lượt thích the others suggest though... =)


Thanks for contributing an answer lớn Stack Overflow!

Please be sure to lớn answer the question. Provide details và share your research!

But avoid

Asking for help, clarification, or responding lớn other answers.Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.


Post Your Answer Discard

By clicking “Post Your Answer”, you agree lớn our terms of service, privacy policy và cookie policy


Not the answer you're looking for? Browse other questions tagged jquery or ask your own question.


How vị you remove all the options of a select box and then add one option và select it with jQuery?
Site kiến thiết / biểu tượng logo © 2022 Stack Exchange Inc; user contributions licensed under cc by-sa. Rev2022.5.20.42186


Your privacy

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device và disclose information in accordance with our Cookie Policy.