类型'string |的参数编号字符串[]'不可分配给'字符串|类型的参数编号

时间:2019-04-12 07:05:18

标签: jquery

我编写了一个jQuery代码来确定高度和宽度并单击功能。代码如下:

$(document).ready(function(){
      console.log("Jquery is working!");
      adjustBar();
      $(window).on('resize', function() {
        adjustBar();
      })
    $('#height').on('input change', function() {
        var height = $(this).val();
        if (height >= 30) {
        var leftOffset = (Math.tan(45 * (Math.PI / 180)) * (height / 2) + 3) * -1;
        $('.steps').css('height', height).css('line-height', height + "px").css('left', leftOffset + "px");
        adjustBar();
      }
    });
    $('.steps').on('click', function() {
      $('.steps').removeClass('active');
      $(this).addClass('active');
    })
  });
  function adjustBar() {
      var items = $('.steps').length;
      var elHeight = $('.steps').height() / 2; //Division by 2 because each pseudo which is skewed is only 50% of its parent.
      var skewOffset = Math.tan(45 * (Math.PI / 180)) * elHeight;
      var reduction = skewOffset + ((items - 1) * 4);
      var leftOffset = $('.steps').css('left').replace('px', '');
      var factor = leftOffset * (-1) - 2;
      $('.steps').css({
        'width': '-webkit-calc((100% + 4px - ' + reduction + 'px)/' + items + ')'
      }); // 4px for borders on either side
      $('.steps:first-child, .steps:last-child').css({
        'width': '-webkit-calc((100% + 4px - ' + reduction + 'px)/' + items + ' + ' + factor + 'px)'
      }); // 26px because to make up for the left offset. Size of last-child is also increased to avoid the skewed area on right being shown  
      $('.steps span').css('padding-left', (skewOffset + 15) + "px");
      $('.steps:first-child span, .steps:last-child span').css({
        'width': '-webkit-calc(100% - ' + factor + 'px)'
      });
    }  

运行该代码时,出现以下错误:

  

错误TS2362:算术运算的左侧必须为“ any”,“ number”,“ bigint”或枚举类型。   event-viewer / event-viewer.component.ts(34,35):错误TS2345:类型'string |编号字符串[]'不可分配给'字符串|类型的参数编号((this:HTMLElement,index:number,value:string)=>字符串|数字| void)'。     类型'string []'不能分配给类型'string |编号((this:HTMLElement,index:number,value:string)=>字符串|数字| void)'。       类型'string []'不能分配给类型'string'。   event-viewer / event-viewer.component.ts(49,20):错误TS2362:算术运算的左侧必须为'any','number','bigint'或枚举类型。

似乎heightwidthleftoffset被认为是字符串,但它们应该是根据元素在网页中的位置计算出的数值。我该如何解决这些错误?

1 个答案:

答案 0 :(得分:1)

您应该将字符串值转换为数字。 您可以使用:var height = Number($(this).val());