0

有人可以向我解释 .Slice 在这段代码中的假定用途吗?如何选择 padding-bottom 的子集?非常感谢。

$(this).bind('click', function(){
      var tid = $(this).attr('id');
      tid = tid.replace('play-button-', '');
      playheight = parseInt($('#play-view-' + tid, 10).height());
      playpadding = parseInt($('#play-view-' + tid, 10).css('padding-bottom').slice(0, -2));
      var flex_height = playheight + playpadding;
      setTimeout(function(){
        $('.flex-viewport').animate({height: flex_height});
      },200);
    });

--- 不使用切片的替代方案 ----

    $(this).bind('click', function(){
      var tid = $(this).attr('id');
      tid = tid.replace('playlist-button-', '');
      playheight = $('#playlist-display-' + tid).height();
      playheight = parseInt(playheight, 10);
      playpadding = ('#playlist-display-' + tid);
      playpadding = $(playpadding).css('padding-bottom');
      if (playpadding != null) {
        playpadding = parseInt(playpadding, 10);
        flex_height = playheight + playpadding;
      } else {
        flex_height = playheight;
      }
        setTimeout(function(){
        $('.flex-viewport').animate({height: flex_height});
      },200);
    });
4

1 回答 1

1

对字符串进行切片获取子字符串并返回它。

在这种情况下,它获取填充值并删除最后两个字符(单位 - 例如 px 或 em),因此 ParseInt 可以将字符串解析为整数。

'10px'.slice(0,-2)将返回“ 10

您可以检查此答案与 substring 函数的一些差异:

于 2013-10-10T07:45:42.453 回答