我需要帮助理解 js 语法

发布于 2024-09-28 02:10:13 字数 1896 浏览 1 评论 0原文

在开始阅读之前...您应该知道下面有很多问题...我将不胜感激任何有助于理解 MIT 代码(jQuery v0.4 的 jFlip 插件)的任何部分的帮助,我发现这很难理解

您可以如果您愿意,可以在 http://plugins.jquery.com/project/jFlip 找到代码

并在 http://www.jquery.info/scripts/jFlip/demo 查看它的工作情况.html

< script type="text/javascript>

(function($){    
       $(function(){    
           $("#g1").jFlip(800,800,{background:"green",cornersTop:false}).
           bind("flip.jflip",function(event,index,total){
              $("#l1").html("Image "+(index+1)+" of "+total);
           });        
       });
   })(jQuery);

< /script>

  1. 该函数获取一个 $,然后在另一个函数之前使用 $?!然后它看起来就像一个函数 $("g1") ...感觉就像一个委托(是吗?它是如何工作的)
  2. 绑定是如何工作的......它是一个js函数,对吧?(我的意思是语言的一部分)
  3. “g1”角色是什么?在代码中,但找不到...

我发现很难的另一段代码是:

; (function($) {
      var Flip = function(canvas, width, height, images, opts) {
      //private vars
      opts = $.extend({ background: "green", cornersTop: true, scale: "noresize" }, opts);
      var obj = this,
      el = canvas.prev(),
  1. 为什么在他运行之前需要“;”,
  2. 在 var 中有一个 var - 这是什么意思 - 它是一个类, struct 还是什么?
  3. el = canvas.prev() ... el 没有在任何地方定义,它是某个东西的保存词吗?

最后一个对我来说很重要:

.click(function(){
  if(onCorner && !flipping) {
      el.trigger("flip.jflip",[index,images.length]);
  }
  return false;
})
  1. 点语法是什么: .click(...一些函数定义...)
  2. 我需要使 click 中的代码每 5 秒执行一次...例如 onPageLoad(while(true) set timeout=5000; call click;)

现在是感谢 Trufa 的好时机链接: 如何使用 JavaScript 获得翻书效果

谢谢就这么多时间了 阿萨夫

Before you start reading... You should know that there are many questions below... I will appreciate any help to understand any part of the MIT code(jFlip plugin for jQuery v0.4) which I find very hard to understand

You can find the code if you like at http://plugins.jquery.com/project/jFlip

And see it working at http://www.jquery.info/scripts/jFlip/demo.html

< script type="text/javascript>

(function($){    
       $(function(){    
           $("#g1").jFlip(800,800,{background:"green",cornersTop:false}).
           bind("flip.jflip",function(event,index,total){
              $("#l1").html("Image "+(index+1)+" of "+total);
           });        
       });
   })(jQuery);

< /script>

  1. the function gets a $ and than uses the $ before another function?! and than it looks like it becomes a function $("g1") ... it feels like a delegate (is it? how does it work)
  2. How does the bind work...it's a js function right? (I mean part of the language)
  3. what is the "g1" role? I would expect something like "select case" somewhere in the code, but can't find one...

Another peace of code that I find hard is:

; (function($) {
      var Flip = function(canvas, width, height, images, opts) {
      //private vars
      opts = $.extend({ background: "green", cornersTop: true, scale: "noresize" }, opts);
      var obj = this,
      el = canvas.prev(),
  1. why is the ";" needed before he function
  2. there is a var within a var - what does it mean- it's a class, struct or what?
  3. el = canvas.prev() ... el is not defined anywhere is it a saved word for something?

and last is the one that is important for me:

.click(function(){
  if(onCorner && !flipping) {
      el.trigger("flip.jflip",[index,images.length]);
  }
  return false;
})
  1. what is the dot syntax : .click(... some function definition...)
  2. I need to make the code in the click to execute every 5 seconds ...like onPageLoad(while(true) set timeout=5000; call click;)

That's a good time to thank Trufa for the those links:
How to get the bookflip effect with JavaScript

Thank you so much for your time
Asaf

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱的那么颓废 2024-10-05 02:10:14

正如 Sarfraz 所建议的,您需要阅读有关 Jquery 的手册。

我将回答您的问题以快速帮助您:

  • $ 使用表示它是一个 Jquery 对象。因此您可以访问 Jquery 提供的所有方法/属性等。

  • $("#g1") 不是委托,是在 jquery 对象中转换 id "g1" 的 html 的方法。

  • “;”在“; (function($)”之前,我认为您不需要它。

  • “有a var inside a var”,我想你指的是这部分“{background:“green”, c”,这是一种将由不同变量组成的对象传递给方法/类的方法。

  • “el”是用“obj”定义,完整句子为“var obj, el=...”,所以定义为变量。

As Sarfraz suggests, you need to read a manual about Jquery.

I will answer your questions to help you quickly:

  • the $ use indicates it is a Jquery object. So you can access all the methods / properties etc... that Jquery provides.

  • the $("#g1") is not a delegate, is the way to convert the html with the id "g1" in a jquery object.

  • the ";" before "; (function($)" I don't think you need it.

  • "there is a var within a var", I suppose you refer to this part "{ background: "green", c", this is a way of passing an object composed of different variables to a method / class.

  • "el" is defined with "obj", the full sentence is "var obj, el=...", so is defined as a variable.

愛放△進行李 2024-10-05 02:10:13

1 个问题

这里你有一个自动执行的匿名函数 (function(){})(); 并且在其中传递 jQuery 对象(实际上是一个函数)作为名为 $ 的变量。这是相同的(称为 LALA 而不是 $):

(function(LALA){    
       LALA(function(){    
           LALA("#g1").jFlip(800,800,{background:"green",cornersTop:false}).
           bind("flip.jflip",function(event,index,total){
              LALA("#l1").html("Image "+(index+1)+" of "+total);
           });        
       });
   })(jQuery);

Bind 在 jQuery 对象中作为单独的子函数(方法)实现。其中有更复杂的代码处理不同浏览器中的事件,但确保您的事件与您用来查看页面的内容无关。

这个#g1是CSS样式选择器。您必须在谷歌上搜索以获取更多信息。这意味着“获取 ID='g1' 的 HTML 元素并对其调用以下方法”。在您的情况下,您选择带有 ID=g1 的元素并对其调用 jFlip() 。要选择所有具有 ELEMENT 类的元素,您可以使用像 $(".ELEMENT") 这样的点。

2 问题

仅当您在同一行中编写两个或多个语句时才需要分号,如下所示:

alert("lalala") var a = 2+3

因此您必须将它们写在单独的行中,如下所示:

alert("lalala") 
var a = 2+3

或者用 ; 分隔在同一行:

alert("lalala"); var a = 2+3

对于变量,你必须知道 JS 中没有类型。您可以在 var、整数、字符串...和对象中使用函数。对象由使用关键字 new 的函数组成。在您的代码中,您将引用 Flip 保存到执行某些操作的匿名函数...不需要声明变量 var,因此您可以使用 el = 10 这就是一个有效的变量定义。

3 问题

点语法不存在。您正在对之前一行中的 . 之前的 jQuery 对象调用 .click()

为了超时,你必须有更多的XP...谷歌更多...

1 Question

Here you are having anonymous function that auto executes (function(){})(); and in it you are passing jQuery object (which is actually a function) as variable that's called $. This is the same (calling it LALA and not $):

(function(LALA){    
       LALA(function(){    
           LALA("#g1").jFlip(800,800,{background:"green",cornersTop:false}).
           bind("flip.jflip",function(event,index,total){
              LALA("#l1").html("Image "+(index+1)+" of "+total);
           });        
       });
   })(jQuery);

Bind is implemented in jQuery object as separate sub function (method). There is more complicated code going on in it that handles events in different browsers, but makes sure that your event gets bind no meter what you use to view the page.

This #g1 is CSS style selector. You will have to google on that for more info. It means "get the HTML element which has ID='g1' and call the following method on it". In you case you are selecting element with ID=g1 and calling jFlip() on it. To select elements which all have class ELEMENT, you would use dot like so $(".ELEMENT").

2 Question

Semicolon is needed only if you write two or more statements in same row like this:

alert("lalala") var a = 2+3

So you must write them in separate rows, like so:

alert("lalala") 
var a = 2+3

Or separate with ; in the same row:

alert("lalala"); var a = 2+3

For vars you will have to know that there are no types in JS. You can have function in var, integer, string... and objects. Objects are made of functions using keyword new. There in your code you are saving reference Flip to anonymous function that does something... To declare variables var is not needed, so you can have el = 10 and that stands as a valid variable definition.

3 Question

Dot syntax doesnt exist. You are calling .click() on jQuery object that was before the . in a line before.

For timeout you will have to have some more XP... google some more...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文