Home  >  Article  >  Web Front-end  >  How to determine whether the specified style exists in jquery

How to determine whether the specified style exists in jquery

藏色散人
藏色散人Original
2021-02-18 10:05:464135browse

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>

jquery code is as follows:

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

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!

Statement:
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
Previous article:Does react require node?Next article:Does react require node?