Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>jQuery has the hasClass() function that returns true if any element in the wrapped set contains the specified class</p> <pre><code>if (!$(this).hasClass("selected")) { //do stuff } </code></pre> <p><a href="http://jsbin.com/ugaxe" rel="noreferrer">Take a look at my example of use</a></p> <ul> <li>If you hover over a div, it fades as normal speed to 100% opacity if the div does not contain the 'selected' class</li> <li>If you hover out of a div, it fades at slow speed to 30% opacity if the div does not contain the 'selected' class</li> <li>Clicking the button adds 'selected' class to the red div. The fading effects no longer work on the red div</li> </ul> <p>Here is the code for it</p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&gt; &lt;head&gt; &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"&gt;&lt;/script&gt; &lt;title&gt;Sandbox&lt;/title&gt; &lt;meta http-equiv="Content-type" content="text/html; charset=utf-8" /&gt; &lt;style type="text/css" media="screen"&gt; body { background-color: #FFF; font: 16px Helvetica, Arial; color: #000; } &lt;/style&gt; &lt;!-- jQuery code here --&gt; &lt;script type="text/javascript"&gt; $(function() { $('#myButton').click(function(e) { $('#div2').addClass('selected'); }); $('.thumbs').bind('click',function(e) { alert('You clicked ' + e.target.id ); } ); $('.thumbs').hover(fadeItIn, fadeItOut); }); function fadeItIn(e) { if (!$(e.target).hasClass('selected')) { $(e.target).fadeTo('normal', 1.0); } } function fadeItOut(e) { if (!$(e.target).hasClass('selected')) { $(e.target).fadeTo('slow', 0.3); } } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="div1" class="thumbs" style=" background-color: #0f0; margin: 10px; padding: 10px; width: 100px; height: 50px; clear: both;"&gt; One div with a thumbs class &lt;/div&gt; &lt;div id="div2" class="thumbs" style=" background-color: #f00; margin: 10px; padding: 10px; width: 100px; height: 50px; clear: both;"&gt; Another one with a thumbs class &lt;/div&gt; &lt;input type="button" id="myButton" value="add 'selected' class to red div" /&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strong>EDIT:</strong></p> <p>this is just a guess, but are you trying to <a href="http://jsbin.com/adana" rel="noreferrer">achieve something like this</a>? </p> <ul> <li>Both divs start at 30% opacity</li> <li>Hovering over a div fades to 100% opacity, hovering out fades back to 30% opacity. Fade effects only work on elements that don't have the 'selected' class</li> <li>Clicking a div adds/removes the 'selected' class</li> </ul> <p>jQuery Code is here-</p> <pre><code>$(function() { $('.thumbs').bind('click',function(e) { $(e.target).toggleClass('selected'); } ); $('.thumbs').hover(fadeItIn, fadeItOut); $('.thumbs').css('opacity', 0.3); }); function fadeItIn(e) { if (!$(e.target).hasClass('selected')) { $(e.target).fadeTo('normal', 1.0); } } function fadeItOut(e) { if (!$(e.target).hasClass('selected')) { $(e.target).fadeTo('slow', 0.3); } } &lt;div id="div1" class="thumbs" style=" background-color: #0f0; margin: 10px; padding: 10px; width: 100px; height: 50px; clear: both; cursor:pointer;"&gt; One div with a thumbs class &lt;/div&gt; &lt;div id="div2" class="thumbs" style=" background-color: #f00; margin: 10px; padding: 10px; width: 100px; height: 50px; clear: both; cursor:pointer;"&gt; Another one with a thumbs class &lt;/div&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload