Home > Web Front-end > Front-end Q&A > How does jquery determine whether an element has a disabled attribute?

How does jquery determine whether an element has a disabled attribute?

WBOY
Release: 2022-05-12 11:17:25
Original
4203 people have browsed it

Judgment method: 1. Use "the element that needs to be judged.attr("disabled");" to obtain the disabled attribute value of the element; 2. Use "if(to obtain the disabled attribute value of the element == "disabled" ){The element has the code for this attribute;}" to make a judgment.

How does jquery determine whether an element has a disabled attribute?

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

How jquery determines whether an element has a disabled attribute

1. Use the attr method to obtain the disabled attribute of an attribute

attr() method sets or returns the attribute Select the attributes and values ​​of the element.

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.

Syntax

Return the value of the attribute:

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

2. Use if loop to make judgments

if 语句
    if(条件表达式){
        语句...
    }
Copy after login

if statement is executed , the conditional expression will be evaluated first.

If the value of the conditional expression is true, the statement after the if will be executed.

If the value of the conditional expression is false, then The statement after if will not be executed

The if statement can only control the statement that follows it

If you want the if statement to control multiple statements, you can put these statements into a code block The code block after the {}

if statement is not necessary, but try to write a code block during development, even if there is only one statement after the if.

The example is as follows:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>123</title>
<script src="js/jquery.min.js">
</script>
<script>
$(document).ready(function(){
var res=$("#mybutton").attr("disabled");
if(res=="disabled"){
alert(&#39;该元素有disabled属性&#39;);
}
});
</script>
</head>
<body>
<button id="mybutton" type="button" disabled>有disabled属性的元素</button>
</body>
</html>
Copy after login

Output result:

How does jquery determine whether an element has a disabled attribute?

How does jquery determine whether an element has a disabled attribute?

##Related Video tutorial recommendation:

jQuery video tutorial

The above is the detailed content of How does jquery determine whether an element has a disabled attribute?. 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