Can jquery get the content of the id in the element?

WBOY
Release: 2022-06-08 10:27:19
Original
1422 people have browsed it

jquery can get the content of the id in the element; it can be obtained by using the attr() method. This method is used to set or return the attributes and values ​​of the selected element. When used to return the attribute value of the specified element, return is just the value of the first matching element, the syntax is "$(selector).attr("id")".

Can jquery get the content of the id in the element?

The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.

jquery gets the content of the element id

jquery can get the content of the id in the element

attr() method sets or returns the attributes of the selected element and value.

When this method is used to return an attribute value, the value of the first matching element is returned.

When this method is used to set attribute values, one or more attribute/value pairs are set for the matching element.

The syntax is:

Return the value of the attribute

$(selector).attr(attribute)
Copy after login

Syntax expansion

Set the attribute and value :

$(selector).attr(attribute,value)
Copy after login

Use functions to set properties and values:

$(selector).attr(attribute,function(index,currentvalue))
Copy after login

Set multiple properties and values:

$(selector).attr({attribute:value, attribute:value,...})
Copy after login

The example is as follows:

Get elements The value of the id attribute.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>123</title>
<script src="js/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
alert("图片的id属性值: " + $("img").attr("id"));
});
});
</script>
</head>
<body>
<img src="img_pulpitrock.jpg" alt="Pulpit Rock" width="284"    style="max-width:90%" id="picture">
<br>
<button>返回图片的id属性值</button>
</body>
</html>
Copy after login

Output result:

Can jquery get the content of the id in the element?

After clicking the button:

Can jquery get the content of the id in the element?

Video tutorial recommendation: jQuery video tutorial

The above is the detailed content of Can jquery get the content of the id in the element?. 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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template