简体   繁体   English

如何用jQuery重写此内联JavaScript?

[英]how to rewrite this this inline javascript with jquery?

I have the following markup with inline javascript and would like to change it to Jquery. 我使用内联javascript进行了以下标记,并希望将其更改为Jquery。 Any help would be appreciated. 任何帮助,将不胜感激。

<a title="823557" href="/PhotoGallery.asp?ProductCode=471823557" id="product_photo_zoom_url">
<img border="0" onload="vZoom.add(this, '/v/vspfiles/photos/471823557-2.jpg');"
alt="823557"
src="/v/vspfiles/photos/471823557-2T.jpg" id="product_photo"></a>

I guess I would need to use this? 我想我需要用这个吗?

$(function(){
<---- somecode---->
});
$(function () {
   $("#product_photo").load(function (e) {
      vZoom.add(this, this.src.replace('T.', '.'));
   })
})();

If $ doesn't work for some reason, this should also work. 如果$由于某种原因无法使用,这也应该适用。 I incorporated Kranu's advice since that library most likely only needs the DOM loaded as a prerequisite, rather than the load event: 我采纳了Kranu的建议,因为该库很可能只需要将DOM作为先决条件而不是load事件:

jQuery(function ($) {
    $("#product_photo").each(function () { // in case there is more than one 
        vZoom.add(this, this.src.replace('T.', '.'));
    });
});

Note that there is no need to put a separate bind event on the img because the $(function() { }) waits until the body loads. 请注意,不需要在img上放置单独的绑定事件,因为$(function() { })会等到主体加载为止。

$(function(){
    vZoom.add(document.getElementById('product_photo'),'/v/vspfiles/photos/471823557-2.jpg');
});

Not exactly sure what you are trying to do, but essentially you would remove the image from the HTML and dynamically load it using JS. 不确定要执行的操作,但实际上您会从HTML中删除图像并使用JS动态加载。 Once loaded, you would inject it in the DOM and set the onload event. 加载后,您可以将其注入DOM并设置onload事件。

var jsImg = new Image();
jsImg.onload = function(){
  vZoom.add(this,'/v/vspfiles/photos/471823557-2.jpg');
  var img = document.createElement('IMG');
  img.setAttribute('id','product_photo');
  img.setAttribute('alt','823557');
  img.setAttribute('src','/v/vspfiles/photos/471823557-2T.jpg');
  img.style.border = 'none';
  //inject image now in the DOM whereever you want.
};
jsImg.src = '/v/vspfiles/photos/471823557-2T.jpg';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM