神奇网站Javascript初始化代码块v1

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了神奇网站Javascript初始化代码块v1相关的知识,希望对你有一定的参考价值。

Link body class names to trigger javascript. It requires jQuery.
  1. $(function(){
  2. //jquery references the body for grabbing class info and for general awesomness
  3. var body = $('body');
  4. jQuery.fx.interval = 5;
  5. //apply all methods as root level functions
  6. var root = this;
  7. //counter
  8. var i;
  9.  
  10. // the goods are in here.
  11. var pageScripts = {
  12. //init is called automatically
  13. //use to set up global stuff
  14. init:function(){
  15. }
  16.  
  17. };
  18. //determine if any of the pageScripts match the class of the body and call it/them
  19.  
  20. //keep track of how many properties have been executed
  21. var numRan = 0;
  22. //split up the classes used in the body
  23.  
  24.  
  25. //array of classes used in the body's class attribute
  26. var bodyClasses = body.attr("class").split(" ");
  27.  
  28. //call init
  29. pageScripts.init();
  30.  
  31. //loop through and execute all
  32. for(i in bodyClasses){
  33. if(pageScripts[ i ]){
  34. numRan++;
  35. pageScripts[ i ].apply(root, []);
  36. }
  37. }
  38.  
  39. log(numRan + " methods have been executed");
  40. });

以上是关于神奇网站Javascript初始化代码块v1的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript 块v1.0

黑科技小代码

Java静态块学习

Java静态块学习

那些短小精悍的&奇葩的&令人感到惊讶的JavaScript代码----更新中

一行神奇的javascript代码