Thursday, 15 September 2011

javascript - jQuery matching a single class against predefined list where element has multiple classes -


I am trying to find out that .hasClass () For whether the active ingredient I am currently working with one of the four distinct classes, I am trying to find out the most favorable method to do this, whereas the element (t) which triggers (or active element) For most style purposes, there are several squares in it

Example of HTML:

  & lt; Div class = "form_row" & gt; & Lt; Div class = "primary_ro" & gt; 1 & lt; / Div & gt; & Lt; / Div & gt; & Lt; Div class = "form_row" & gt; & Lt; Div class = "primary_o sub-expert" & gt; 1 & lt; / Div & gt; & Lt; Div class = "primary_row_sub" & gt; 1 a & lt; / Div & gt; & Lt; / Div & gt; & Lt; Div class = "form_row" & gt; & Lt; Div class = "primary_o sub-expert" & gt; 1 & lt; / Div & gt; & Lt; Div class = "primary_row_sub sub-specialist" & gt; 1a & lt; / Div & gt; & Lt; Div class = "secondary_row" & gt; 2 & lt; / Div & gt; & Lt; / Div & gt; & Lt; Div class = "form_row" & gt; & Lt; Div class = "primary_o sub-expert" & gt; 1 & lt; / Div & gt; & Lt; Div class = "primary_row_sub sub-specialist" & gt; 1a & lt; / Div & gt; & Lt; Div class = "secondary_o sub-expert" & gt; 2 & lt; / Div & gt; & Lt; Div class = "secondary_row_sub" & gt; 2 a & lt; / Div & gt; & Lt; / Div & gt; I am currently in the process of preparing this, so it is still a difficult draft, but according to the requirement it will be safe to assume more classes on various elements. There are four main classes about which I am worried about  primary_ro, primary_OR_sub, secondary_or, secondary_or_sub_sub.b./ . I am creating a click handler such as:  
  $ ('.form_row & gt; div'). Click (function () {// code});   

In this click handler I want to be able to figure out that element has been clicked one of the four described above. Where it is, I want to do something based on that. So instead of constructing four click operators for each type, determine what is the element of the element. I hope I can customize it and I can justify the same handler. Any ideas?

one option:

  var classMap = {"one": Work () {alert ("one");}, "two": work () {alerts ("two");}, "three": function () {alert ("three")}, class = " "; $ ('Div') Click (function (e) {classes = this.className.split (""); for (key in clamsamp) {if ($ .inArray (key, class)! == -1) {Classmap [key] ( );}}});   



No comments:

Post a Comment