hasClassLike

hasClassLike
 点击下载

hasClassLike allows the selection of elements based on pattern matching rather than specific names. This allows, for example, the passing of parameters to JavaScript via class names:

  1. /* in js, such as in $(document).ready */ 
  2. $("*") 
  3.         .hasClassLike("integer") /* filter some before using regexp */ 
  4.         .hasClassLike(/^integer(?:Min(-?\d+))?(?:Max(-?\d+))?$/,function(m) { 
  5.                 var mmin = m[1]; 
  6.                 var mmax = m[2]; 
  7.                 // ... install validating event handlers on $(this) ... 
  8.         }); 
  9. <!-- in HTML --> 
  10. <input type="text" class="someOtherClass integerMin0"/> 
  11. <input type="text" class="integerMin-100Max100 someOtherClass"/> 
  12. <input type="text" class="someOtherClass integer yetAnotherClass"/> 

There is an efficient simple substring match .hasClassLike(str) matched against the entire className of the element (that is, the space-separated list), useful for paring out unlikely elements. Then, a RegExp match with .hasClassLike(rx) comes back with the elements bearing at least one class matching a given pattern. Adding a callback function with .hasClassLike(rx,fn) makes it easy to modify or filter the found elements based on the match, which is passed to the callback function. If the callback returns false, the element is not included in the output (like with .filter).

 

转载请注明:代码家园 » hasClassLike

评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)