拆分并删除href属性中的特定值,并使用jquery添加新值

拆分并删除href属性中的特定值,并使用jquery添加新值,jquery,html,Jquery,Html,我已经拆分了标记的href属性中的值 我拆分了该值,但无法删除这些值并向其中添加新值 我的HTML代码是 <a class="chapter_video" href="http://localhost/SVE128-portal2/public/video/9/25"></a> 我试过了 var remove = href.replace('split[7]', '') 而不是href.remove(拆分[7])这很简单: var newUrl = 4; v

我已经拆分了标记的href属性中的值

我拆分了该值,但无法删除这些值并向其中添加新值

我的HTML代码是

<a class="chapter_video" href="http://localhost/SVE128-portal2/public/video/9/25"></a> 
我试过了

 var remove = href.replace('split[7]', '') 
而不是
href.remove(拆分[7])

这很简单:

 var newUrl = 4;
 var video_url = $('.chapter_video');
 var href = video_url.attr("href");
 //splitting value of href                  
 var split = href.split("/");
 if(split[7] !== undefined){

     href = href.replace(split[7], '');
 }

 video_url.attr('href', href + newUrl);
var video_url=$('.chapter_video');
var href=video_url.attr(“href”);
var split=href.split(“/”);
使用这个;
var addthis=4;
如果(拆分[7]!==未定义){
usethis=split.slice(0,-1);
}
console.log(使用this.join(“/”+“/”+addthis)

  • 不要对变量名使用new关键字
  • 其中没有字符串的删除函数。(使用replace('val','')进行删除)

  • 旧版本: 虽然穆罕默德·卡西姆(Muhammad Qasim)的答案解决了您的问题,但它没有考虑到
    split[7]
    的值可能在字符串中出现多次。我相信这会以正确的方式解决您的问题:

    var new = 4;
    var video_url = $('.chapter_video');
    var href = video_url.attr("href");
    // Splitting value of href                  
    var split = href.split("/");
    if(split[7] !== undefined){                         
        // This removes the last item of the array
        // You could also use .pop() which also removes the last part of the array
        split.splice(-1, 1);
    }
    // Pastes all the array parts back together (opposite of split)
    href = split.join('/')
    
    video_url.attr('href', href + '/'+ new);
    
    新版本 经过更多的测试,我意识到拼接并不是最聪明的方法。直接替换URL中的值会更好:

    var new_id = 4;
    var video_url = $('.chapter_video');
    var href = video_url.attr("href");
    // Splitting value of href         
    var split = href.split("/");
    if(split[7] !== undefined){
        split[7] = new_id;
    }
    else {
        // If there isn't a 8th item in the array, add it to the end
        split.push(new_id)
    }
    // Pastes all the array parts back together (opposite of split)
    href = split.join('/')
    
    video_url.attr('href', href);
    

    如果url是从服务器端生成的,那么这种解决问题的方法并不是未来的证明,如果url超时更改会怎么样? 更准确的方法是将url的静态部分放在不同的属性中:

    <a 
      class="chapter_video" 
      href="http://localhost/SVE128-portal2/public/video/9/25" 
      data-href="http://localhost/SVE128-portal2/public/video"
      data-pid="9"
      data-id="25"
    >
    </a>
    
    var new = 4;
    var video_url = $('.chapter_video');
    var href_part = video_url.attr("data-href");
    var pid = video_url.attr("data-pid");
    
    video_url.attr('href', href_part + '/'+ pid + '/' + new);
    
    
    var new=4;
    var video\u url=$(“.chapter\u video”);
    var href_part=video_url.attr(“数据href”);
    var pid=video_url.attr(“数据pid”);
    视频(url.attr('href',href_part+'/'+pid+'/'+new));
    
    您是要删除最后一部分,还是始终删除某个特定部分?specific part@Douwedehaann没有将其作为新值追加而不删除我已更新了答案。请仔细检查。尤其是最后一条线所有功能正常,但其追加的额外斜线拼接(0,-1)不会删除我端的任何内容。我相信你的意思是
    splice(-1)
    ?@DouwedeHaan这就是我一直在使用的,它确实删除了。你在使用什么浏览器?当我用Google Chrome(56)测试它时,它什么也不做。@DouwedeHaan
    Version 57.0.2987.133
    我的最终答案(最新版本)没有使用拼接。我突然想到,id后面可能有更多的url部分,这会破坏我们的脚本。但我要研究一下拼接版本的区别。谢谢它起作用了。。多谢各位much@Vinothini我添加了更多的信息和更新的版本。如果您的url在示例url中的25之后包含更多部分,我的旧版本将崩溃。
    var new_id = 4;
    var video_url = $('.chapter_video');
    var href = video_url.attr("href");
    // Splitting value of href         
    var split = href.split("/");
    if(split[7] !== undefined){
        split[7] = new_id;
    }
    else {
        // If there isn't a 8th item in the array, add it to the end
        split.push(new_id)
    }
    // Pastes all the array parts back together (opposite of split)
    href = split.join('/')
    
    video_url.attr('href', href);
    
    <a 
      class="chapter_video" 
      href="http://localhost/SVE128-portal2/public/video/9/25" 
      data-href="http://localhost/SVE128-portal2/public/video"
      data-pid="9"
      data-id="25"
    >
    </a>
    
    var new = 4;
    var video_url = $('.chapter_video');
    var href_part = video_url.attr("data-href");
    var pid = video_url.attr("data-pid");
    
    video_url.attr('href', href_part + '/'+ pid + '/' + new);