【问题标题】:How to use Split Function with matching functions如何将拆分函数与匹配函数一起使用
【发布时间】:2020-12-21 15:29:18
【问题描述】:

我尝试为我的项目使用具有匹配功能的拆分功能代码,但我没有完美地工作,我想借助 jquery 和拆分功能代码在我的网站上使用

这里是代码示例

  $(".Menu > li > a").each(function() {
             var $this = $(this),
            sp = $this.attr("href").split("/");
            if(!isNaN(sp[0])){
              var count = sp[0],
                  type = sp[1],
                  label = sp[2];
              blockfeeds($this, count, label, type);
            }
    });

当我借助匹配的短代码 10/Life/Wrapper 运行此代码时,它工作正常,但拆分功能存在问题 我需要我使用这个快捷代码来完美地工作这个代码,这是我需要的简码 Life/Wrapper

但我尝试使用此短代码 Life/Wrapper 来工作 这是示例

      $(".Menu > li > a").each(function() {
             var $this = $(this),
            sp = $this.attr("href").split("/");
            if(!isNaN(sp[0])){
              var type = sp[1],
                  label = sp[2];
              blockfeeds($this, 5, label, type);
            }
    });

在这里,当我尝试上面的代码并将 count = sp[0], 替换为 count = 5, 时,它工作正常,但短代码是 /Life/Wrapper 然后它工作 为什么我运行代码时有'/'

任何机构请帮助我或指导我如何从短代码中删除这个“/”/Life/Wrapper

这样我就可以在没有任何 / 的情况下使用 Life/Wrapper 开始

匹配此简码的 HTML 代码在这里

<div class="Menu">
<li>
<a href="Life/Wrapper">Menu</a>
</li>
</div>

【问题讨论】:

    标签: javascript jquery ajax function split


    【解决方案1】:

    你可以试试这个

    $(".Menu > li > a").each(function() {
        let splits = $(this).attr("href").split("/");
        if(2 === splits.length) {
            let type = splits[0];
            let label = splits[1];
            blockfeeds($(this), 5, label, type);
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-20
      • 1970-01-01
      相关资源
      最近更新 更多