【问题标题】:Argument of type 'string | number | string[]' is not assignable to parameter of type 'string | number |'string | 类型的参数号码 | string[]' 不能分配给“string |”类型的参数号码 |
【发布时间】:2019-04-12 17:23:12
【问题描述】:

我编写了一个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[]' 不能分配给“string |”类型的参数号码 | (((this: HTMLElement, index: number, value: string) => string | number | void)'。 类型 'string[]' 不能分配给类型 'string |号码 | (((this: HTMLElement, index: number, value: string) => string | number | void)'。 类型 'string[]' 不能分配给类型 'string'。 event-viewer/event-viewer.component.ts(49,20):错误 TS2362:算术运算的左侧必须是“any”、“number”、“bigint”或枚举类型。

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

【问题讨论】:

  • 您是否尝试将值转换为数字?例如var height = Number($(this).val());
  • 啊!我没有想到这个选角,谢谢:)
  • 解决问题了吗?然后我可以将其作为答案。
  • 是的,它解决了问题...

标签: jquery


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-10
    • 2021-11-11
    • 2020-04-04
    • 2021-10-02
    • 2021-09-06
    • 1970-01-01
    • 2021-10-06
    • 2020-04-15
    相关资源
    最近更新 更多