How to get, set and delete properties using jQuery

帅杰杰
Release: 2020-05-01 11:32:32
Original
2110 people have browsed it

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","路径"); //获取属性值
Copy after login

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"); //获取图片How to get, set and delete properties using jQuery元素 var path=$img.attr("src"); //获取图片How to get, set and delete properties using jQuery元素节点src属性
Copy after login

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","路径"); //设置图片How to get, set and delete properties using jQuery元素节点src属性值
Copy after login

If you need to set multiple attributes for the same element at once:

$img.attr({"src":"路径","title":"图片提示文字"}); //同时设置同一个元素多个属性
Copy after login

Delete attributes

Delete from document Specific attributes of an element can be achieved using the removeAttr() method.

$("#pto").removeAttr("title");
Copy after login

Implementation results:

旧:How to get, set and delete properties using jQuery 新:How to get, set and delete properties using jQuery
Copy after login

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内容*/ How to get, set and delete properties using jQuery
/*大图*/
/*小图*/ How to get, set and delete properties using jQuery How to get, set and delete properties using jQuery How to get, set and delete properties using jQuery
//jQuery内容 $(function(){ $("div img").mouseover(function(){ var big_src=$(this).attr("src"); //获取小图的src属性 $("#test").attr("src",big_src); //设置大图的src属性 }); });
Copy after login

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.

How to get, set and delete properties using jQuery

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!