Home>Article>Web Front-end> How to get the value of href in a tag with jquery

How to get the value of href in a tag with jquery

王林
王林 Original
2020-11-25 10:12:30 10134browse

Jquery method to get the value of href in a tag: first create a function in the js tag; then get the a tag object through the id in the function, and use the attr method to get the value of the href attribute; finally The obtained value can be output using the alert method.

How to get the value of href in a tag with jquery

The operating environment of this tutorial: Windows 10 system, jquery version 2.2.4. This method is suitable for all brands of computers.

(Learning video sharing:javascript video tutorial)

The specific steps are as follows:

Create a new html file , named test.html, is used to explain how jquery obtains the href value of the a tag.

How to get the value of href in a tag with jquery

In the test.html file, use the a tag to create a link and set its href attribute to "This is the link address".

How to get the value of href in a tag with jquery

In the test.html file, set the id of the a tag to myid, which is mainly used to obtain the a tag object through this id below.

How to get the value of href in a tag with jquery

In the test.html file, use the button tag to create a button with the button name "Get the href value of the a tag".

How to get the value of href in a tag with jquery

In the test.html file, bind the onclick event to the button button. When the button is clicked, execute the myfun() function.

How to get the value of href in a tag with jquery

In the js tag, create the myfun() function. Within the function, obtain the a tag object through id(myid), and use the attr() method to obtain the value of the href attribute. , and output the obtained value using the alert() method.

How to get the value of href in a tag with jquery

Open the test.html file in the browser, click the button to view the results.

How to get the value of href in a tag with jquery

For more programming-related knowledge, please visit:Programming Courses! !

The above is the detailed content of How to get the value of href in a tag with jquery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn