Home > Web Front-end > JS Tutorial > body text

How to determine whether the specified style exists in jquery

藏色散人
Release: 2023-01-03 09:23:39
Original
4136 people have browsed it

The jquery method to determine whether a specified style exists: first create an HTML code sample file; then set or return the attribute value of the selected element through the attr method; finally return a specified string value through the indexOf method. Just find the first occurrence position in the string and judge it.

How to determine whether the specified style exists in jquery

The operating environment of this article: Windows 7 system, jquery version 1.2.6, DELL G3 computer.

How does jquery determine whether the specified style exists?

Let’s take a look at the example below.

HTML code:

<div id="divid" style="font-size:12px;">11111</div> 
<div id="divid" style="">22222</div>
Copy after login

jquery code is as follows:

$("#moment").click(function(){ 
var fontSize = $(this).attr("style").indexOf("font-size"); 
if(fontSize != (-1)){
  console.log("未定义")
} 
else{
  console.log("有定义")
} 
});
Copy after login

attr() method sets or returns the attribute value of the selected element. Depending on the parameters of the method, the way it works is also different.

The indexOf() method returns the position of the first occurrence of a specified string value in a string. Returns -1 if no matching string is found.

Note: The indexOf() method is case-sensitive.

Recommended: "jquery video tutorial"

The above is the detailed content of How to determine whether the specified style exists in 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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template