Home  >  Article  >  Web Front-end  >  jquery sets a note value

jquery sets a note value

WBOY
WBOYOriginal
2023-05-08 22:02:06603browse

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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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!

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