这个简单的javascript是做什么的

时间:2011-11-13 12:00:49

标签: javascript

我正在阅读一个教程,并继续陷入第一行代码,我知道php中的“新”是创建一个对象,但本教程与此无关。请帮忙

  var img = new Image();

教程在这里:

http://jqueryfordesigners.com/image-loading/

好的,我找到了预定义的javascript对象Image()的W3定义:http://www.w3schools.com/jsref/dom_obj_image.asp

4 个答案:

答案 0 :(得分:3)

实际上,在JavaScript中new也是在制作对象时使用的,就像PHP一样。

因此,您可以猜测,该行代码只是实例化Image对象并将其分配给img变量。

答案 1 :(得分:0)

您可以使用

预加载图像
  var img = new Image();

他们预装图像images / headshot.jpg

然后他们等到它被加载

然后隐藏加载图像并淡入图像

答案 2 :(得分:0)

这与jQuery无关。

Image是一个原生JavaScript对象,对应于HTML图像对象。

这条线意味着什么 “创建一个新变量'img'并将其设置为新初始化的Image对象”

答案 3 :(得分:0)

它为你制作了一个HTML格式。

有关详细信息,请阅读此http://www.devguru.com/technologies/ecmascript/quickref/image.html

希望这对你有很大帮助。

或者你想知道它创建

var img = new Image();

而不使用?

$(img)
  // once the image has loaded, execute this code
  .load(function () {
    // set the image hidden by default    
    $(this).hide();

    // with the holding div #loader, apply:
    $('#loader')

    // remove the loading class (so no background spinner), 
    .removeClass('loading')
    // then insert our image
    .append(this);

    // fade our image in to create a nice effect
    $(this).fadeIn();
  })

    // if there was an error loading the image, react accordingly
   .error(function () {
   // notify the user that the image could not be loaded
   })

// *finally*, set the src attribute of the new image to our image
.attr('src', 'images/headshot.jpg');
});

'this'关键字代表img变量

相关问题