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.
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!