In Web development, we often use hyperlinks (a tags) for page navigation. Sometimes, we need to modify or set the attribute value of a tag in JavaScript code, such as href, title, etc. jQuery is a very popular JavaScript library that provides many convenient methods for handling DOM operations. In this article, I will show you how to use jQuery to set the attribute value of a tag.
- Set the href attribute value of the a tag
Changing the link address of the a tag can be achieved through the attr() method in jQuery. This method can get or set the attribute value of the element. The format is as follows:
$('selector').attr('attributeName','value'); //设置属性值
where selector is the element selector, attributeName is the attribute name, and value is the attribute value.
Take setting the href attribute value of an a tag linked to Baidu as an example. The code is as follows:
<a id="myLink" href="http://www.google.com">Google</a> <script> $("#myLink").attr("href", "http://www.baidu.com"); </script>
This code will change the href attribute value of the a tag from Google to Baidu.
- Set the title attribute value of a tag
The title attribute is used to display the text when the mouse hovers over the element. The setting method using jQuery is the same as setting the href attribute. , the code is as follows:
<a id="myLink" href="http://www.baidu.com" title="百度">Baidu</a> <script> $("#myLink").attr("title", "谷歌"); </script>
This code will change the title attribute value of the a tag from Baidu to Google.
- Set the class attribute value of the a tag
The class attribute is used to set the style class, and multiple styles can be applied to the element at the same time. In jQuery, you can use the addClass() and removeClass() methods to add or remove the class attribute. The code is as follows:
<a id="myLink" href="http://www.baidu.com" class="link1 link2">Baidu</a> <script> $("#myLink").addClass("link3"); //添加样式类link3 $("#myLink").removeClass("link2"); //删除样式类link2 </script>
This code will add style class link3 to the a tag and remove style class link2.
- Set the target attribute value of a tag
The target attribute is used to specify the window opened by the link, with optional values such as _blank, _self, _parent, _top, etc. The attribute value can be set using jQuery's attr() method. The code is as follows:
<a id="myLink" href="http://www.baidu.com" target="_blank">Baidu</a> <script> $("#myLink").attr("target", "_self"); </script>
This code will change the target attribute value of the a tag from _blank to _self, and the specified link will be opened in the current window.
- Set the text content of a tag
Sometimes, we need to modify the text content of a tag, which can be achieved through jQuery's text() and html() methods. The text() method is used to set or get the text content of the element, and the html() method is used to set or get the HTML content of the element. The code is as follows:
<a id="myLink" href="#">Link</a> <script> $("#myLink").text("New Link Text"); //修改文本内容 $("#myLink").html("<b>New</b> Link Text"); // 修改HTML内容 </script>
This code will change the text content of the a tag from Link to New Link Text. You can also use the html() method to add a bold effect.
Summary
Through the above examples, we can see that using jQuery’s attr() method can easily modify various attributes of the a tag, thereby better realizing our page interaction. Effect. Compared with JavaScript, jQuery provides a simpler and more efficient API, quickly becoming the first choice for web developers.
The above is the detailed content of jquery sets a note value. For more information, please follow other related articles on the PHP Chinese website!

The article discusses useEffect in React, a hook for managing side effects like data fetching and DOM manipulation in functional components. It explains usage, common side effects, and cleanup to prevent issues like memory leaks.

Lazy loading delays loading of content until needed, improving web performance and user experience by reducing initial load times and server load.

Higher-order functions in JavaScript enhance code conciseness, reusability, modularity, and performance through abstraction, common patterns, and optimization techniques.

The article discusses currying in JavaScript, a technique transforming multi-argument functions into single-argument function sequences. It explores currying's implementation, benefits like partial application, and practical uses, enhancing code read

The article explains React's reconciliation algorithm, which efficiently updates the DOM by comparing Virtual DOM trees. It discusses performance benefits, optimization techniques, and impacts on user experience.Character count: 159

The article explains useContext in React, which simplifies state management by avoiding prop drilling. It discusses benefits like centralized state and performance improvements through reduced re-renders.

Article discusses preventing default behavior in event handlers using preventDefault() method, its benefits like enhanced user experience, and potential issues like accessibility concerns.

The article discusses the advantages and disadvantages of controlled and uncontrolled components in React, focusing on aspects like predictability, performance, and use cases. It advises on factors to consider when choosing between them.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version
Recommended: Win version, supports code prompts!
