How to get, set and delete properties using jQuery
Getting and setting attributes
To get the src attribute of a small picture, set the src attribute value of a large picture, and make the large picture continuously switch, you must master jQuery getting and setting Property methods.
For example: the id of a certain picture is "pto", and the src attribute value can be obtained in JavaScript in the following way.
var img=document.getElementById("pto"); var path=img.src; //获取属性 img.src="路径"; //设置属性值 img.getAttribute("src"); //获取属性 img.getAttribute("src","路径"); //获取属性值
Use the attr() method in jQuery to get and set element attributes.
To get the src attribute of the image, just pass a parameter to the attr() method, which is the attribute name.
var $img=$("#pto"); //获取图片<img src="/static/imghw/default1.png" data-src="01.jpg" class="lazy" alt="How to get, set and delete properties using jQuery" >元素 var path=$img.attr("src"); //获取图片<img src="/static/imghw/default1.png" data-src="01.jpg" class="lazy" alt="How to get, set and delete properties using jQuery" >元素节点src属性
If you want to set the src attribute value of the image, continue to use the attr() method. The difference is that you need to pass two parameters, namely the attribute name and the corresponding value.
$img.attr("src","路径"); //设置图片<img src="/static/imghw/default1.png" data-src="01.jpg" class="lazy" alt="How to get, set and delete properties using jQuery" >元素节点src属性值
If you need to set multiple attributes for the same element at once:
$img.attr({"src":"路径","title":"图片提示文字"}); //同时设置同一个元素多个属性
Delete attributes
Delete from document Specific attributes of an element can be achieved using the removeAttr() method.
$("#pto").removeAttr("title");
Implementation results:
旧:<img src="/static/imghw/default1.png" data-src="01.jpg" class="lazy" title="123" alt="How to get, set and delete properties using jQuery" > 新:<img src="/static/imghw/default1.png" data-src="01.jpg" class="lazy" alt="How to get, set and delete properties using jQuery" >
After mastering the attr() and removeAttr() methods, you can move the mouse to an element to change the attribute value.
Note: The jQuery file must be introduced before it can be applied
/*html内容*/ <img src="/static/imghw/default1.png" data-src="img/img1/fw1.jpg" class="lazy" id="test" title="test"/ alt="How to get, set and delete properties using jQuery" ><br /> /*大图*/ <div> /*小图*/ <img src="/static/imghw/default1.png" data-src="img/img1/22.jpg" class="lazy" / alt="How to get, set and delete properties using jQuery" > <img src="/static/imghw/default1.png" data-src="img/img1/33.jpg" class="lazy" / alt="How to get, set and delete properties using jQuery" > <img src="/static/imghw/default1.png" data-src="img/img1/44.jpg" class="lazy" / alt="How to get, set and delete properties using jQuery" > </div> //jQuery内容 $(function(){ $("div img").mouseover(function(){ var big_src=$(this).attr("src"); //获取小图的src属性 $("#test").attr("src",big_src); //设置大图的src属性 }); });
When you run the program, you will find that when the cursor moves into a certain small picture, The small image will be displayed in the large image display area.
Summary:
Use attr() to set or get attributes and attribute values.
If you want to set multiple attributes in the same element, you need to put the attributes and attribute values in curly brackets. Use colons between attributes and attribute values, and use commas between attributes and attributes.
To delete an attribute, use removeAttr("attribute name") directly.
The above is the detailed content of How to get, set and delete properties using jQuery. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Detailed explanation of jQuery reference method: Quick start guide jQuery is a popular JavaScript library that is widely used in website development. It simplifies JavaScript programming and provides developers with rich functions and features. This article will introduce jQuery's reference method in detail and provide specific code examples to help readers get started quickly. Introducing jQuery First, we need to introduce the jQuery library into the HTML file. It can be introduced through a CDN link or downloaded

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: <

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

Thread of Despair is a rare card in Blizzard Entertainment's masterpiece "Hearthstone" and has a chance to be obtained in the "Wizbane's Workshop" card pack. Can consume 100/400 arcane dust points to synthesize the normal/gold version. Introduction to the attributes of Hearthstone's Thread of Despair: It can be obtained in Wizbane's workshop card pack with a chance, or it can also be synthesized through arcane dust. Rarity: Rare Type: Spell Class: Death Knight Mana: 1 Effect: Gives all minions a Deathrattle: Deals 1 damage to all minions

jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s

jQuery is a popular JavaScript library widely used in web development. During web development, it is often necessary to dynamically add new rows to tables through JavaScript. This article will introduce how to use jQuery to add new rows to a table, and provide specific code examples. First, we need to introduce the jQuery library into the HTML page. The jQuery library can be introduced in the tag through the following code:
