【问题标题】:Where should I store jQuery data for html elements?我应该在哪里存储 html 元素的 jQuery 数据?
【发布时间】:2012-07-15 06:33:30
【问题描述】:

为了更好地学习 jquery,我决定编写一个插件来创建一个 gallery collage effect 就像 google+。这是example

我想在调整包含图像的 html 元素的大小时再次触发它。我遇到的部分问题是我需要存储原始图像大小,以便重新计算图像大小以使其适合。

我不知道在哪里存储以及如何检索这些原始图像大小。完整的插件链接在上面,但我会在这里做一个总结。

;(function( $ ) {
    $.fn.collagePlus = function( options ) {

        var settings = $.extend( 
            //... 
            'images'          : $('img', $(this))
            //... 
        );

        return this.each(function() {
            settings.images.each(function(index){
                //... 

                /*
                * get the current image size
                */
                var w = (typeof $(this).data().width != 'undefined') ? $(this).data().width : $(this).width();
                var h = (typeof $(this).data().height != 'undefined') ? $(this).data().height : $(this).height();

                /*
                * store the original size for resize events
                */
                $(this).attr( "data-width" , w  );
                $(this).attr( "data-height" , h  ); 
                //... Do some other stuff
                }
            );
        });
    }
})( jQuery );

【问题讨论】:

  • 不确定,但您没有以错误的方式使用 .data() 吗?我总是设置像:$(this).data("height", h);这样的数据并像:$(this).data("height");那样检索它而且我很确定,在设置了高度数据属性之后,下次你检索它就不会是未定义的了。
  • @Lumocra 这似乎是问题所在,是的

标签: javascript jquery


【解决方案1】:

你用错了.data()。当您将 1 个参数传递给 .data 函数时,它会返回给定键的值。当您传递 2 个参数时,.data 将为该键设置值。

这个区块:

//get the current image size
var w = (typeof $(this).data().width != 'undefined') ? $(this).data().width : $(this).width();
var h = (typeof $(this).data().height != 'undefined') ? $(this).data().height : $(this).height();

应该是:

var $this = $(this); //caching your selector
if (!$this.data('width')) //if this element doesn't have a width stored
    $this.data('width', $this.width()); //stores currently computed width
if (!$this.data('height')) //repeat
    $this.data('height', $this.height());

当然,稍后检索数据:

alert($this.data('width')) //alerts currently stored width

Fiddle Demo

您还可以在.data 中存储一个对象,传递一个属性映射:

if (!$(this).data('size'))
    $(this).data('size', { width: $(this).width(), height: $(this).height() });

现在widthheight 是存储在.data('size') 中的对象的属性,可以通过以下方式检索:

alert($(this).data('size').width);

Fiddle

为了简单起见,我主要选择第一个选项。但是第二个看起来更整洁。选择您认为更具可读性和可维护性的任何一个。

【讨论】:

  • 谢谢,成功了。 var $this = $(this); 为什么要缓存选择器?这是我应该做的吗?
  • 是的。一般来说,如果你多次使用 $(this),你应该把它缓存在一个变量中。前缀“$”有助于将变量的值识别为 jQuery 包装对象。
  • @ed209 这是一个微优化,我正在寻找 Jared 的 $(this) vs $this 问题,这是一个很好的参考。 +1 @Wolfram,另外,这样你每次引用$this 时都不会创建一个新的 jQuery 对象。重复使用选择器可以提高性能。
  • @ed209 在这里,found it。启发我使用选择器缓存的问题。 :)
  • 感谢@Wolfram 的编辑,当我超过睡眠时间时直接在答案框中编写代码往往会产生一些愚蠢的错误。现在也修复了 Fiddle。 =]
【解决方案2】:

在服务器端,您可以将 HTML 元素的数据存储在 data-* attributes 中,并通过 jQuery 的 .data() 函数获取它(因为 jQuery 1.4.3 也改变了该函数的一般行为,如文档中所述)。您正在插件中设置属性,但此时,您可以将原始宽度和高度存储在 data 对象中,如下所示:

$(this).data( "data-width", w );
$(this).data( "data-height", h );

使用.data() 函数会返回数据,无论它是作为data- 属性存储在您的HTML 中还是包含在附加到元素的jQuery 的data 对象中。您已经在使用不带任何参数的.data() 函数,其中returns the complete data object of the matched elements 还包含来自HTML 属性和来自jQuery 的data 对象的数据。这可行,但您可以通过这样调用它来获取已保存的 widthheight

$(this).data("width");
$(this).data("height");

【讨论】:

  • 是的,我没有提到.data() 还检索标记中设置的初始值,这值得+1。请注意,OP 将 computed 宽度与.width() 客户端一起存储,如果有可能影响它的 CSS 规则(即 % 尺寸,可变父尺寸),它可能与实际图像尺寸不同等),这在服务器端更难解释,否则可能会产生不良影响。
猜你喜欢
  • 2017-10-11
  • 1970-01-01
  • 2011-02-15
  • 1970-01-01
  • 2015-01-22
  • 1970-01-01
  • 2018-12-27
  • 2023-04-07
  • 1970-01-01
相关资源
最近更新 更多