【问题标题】:Is it possible to replace a part of an <a> href values depending on the href value?是否可以根据 href 值替换 <a> href 值的一部分?
【发布时间】:2013-02-14 18:05:32
【问题描述】:

我正在尝试集成 Video JS 和 Colorbox。目标是使用 jQuery 简单地更改所有具有“.mp4”的 href 的值,然后将“.mp4”更改为“.html”。这将允许现有代码工作并将两者集成。通过将“.mp4”更改为“.html”,我可以将 Video JS Player 放置在 html 页面中并在 Colorbox 中加载页面。目前,视频只是在没有播放器的情况下加载到彩盒中。

<a href="http://mysite/files/myvideo.mp4" class="cboxElement">Video</a>


$(document).ready(function(){

    $("a.cboxElement[href^='.mp4']")
   .each(function()
   { 
      this.href = this.href.replace(/^".mp4"/, 
         ".html");
   });


    $(".cboxElement").colorbox({iframe:true, innerWidth:640, innerHeight:370});
});

【问题讨论】:

    标签: jquery html video colorbox href


    【解决方案1】:

    ^ 表示为start with,但在您的情况下不是,它以.mp4 结尾,因此您必须选择以$ 结尾的属性选择器(可能会说) .

    试试这个:http://jsfiddle.net/QK9Ry/

    $("a.cboxElement[href$='.mp4']").each(function () {
       this.href = this.href.replace(".mp4",".html");
    });
    

    【讨论】:

      【解决方案2】:

      没有测试,我认为这行是错误的:

      this.href = this.href.replace(/^".mp4"/, 
           ".html");
      

      ^ 表示以正则表达式开头。尝试这样做:

      this.href = this.href.replace(".mp4", 
           ".html");
      

      【讨论】:

        【解决方案3】:

        只需包装两个答案。你将需要他们两个来使它正确。

        $(function(){
        
        $("a.cboxElement[href$='.mp4']")
         .each(function()
        {
        
          this.href = this.href.replace(".mp4", ".html");
        
        });
        
        
        $(".cboxElement").colorbox({iframe:true, innerWidth:640, innerHeight:370});
        });
        

        http://jsfiddle.net/2A3UP/

        【讨论】:

          猜你喜欢
          • 2012-12-27
          • 1970-01-01
          • 2017-02-26
          • 2017-06-04
          • 2011-09-10
          • 2020-11-22
          • 2014-01-06
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多