【问题标题】:extract and replace (YouTube ONLY) Iframe src提取和替换(仅限 YouTube)Iframe src
【发布时间】:2011-11-07 19:15:18
【问题描述】:

如果 YouTube iframe html 是

<iframe width="560" height="315" src="http://www.youtube.com/embed/VIDEOID" frameborder="0" allowfullscreen></iframe>

jquery 应该测试 iframe src 以查看前 29 个字符是否匹配 http://www.youtube.com/embed/

如果它确实应用 jquery 在/embed/ 之后提取VIDEOID 并将iframe src 替换为http://www.redirect.mysite.com/?id=VIDEOID

所以下面是 iframe 的 src

<iframe width="560" height="315" src="http://www.youtube.com/embed/EhrYQrLBiTQ" frameborder="0" allowfullscreen></iframe>

应该替换为

<iframe width="560" height="315" src="http://www.redirect.mysite.com/?id=EhrYQrLBiTQ" frameborder="0" allowfullscreen></iframe>

【问题讨论】:

    标签: jquery iframe youtube


    【解决方案1】:

    看到这个小提琴:http://jsfiddle.net/maniator/cRFgv/

    代码:

    $('iframe').each(function(){
        this.src = this.src.replace('http://www.youtube.com/embed/', 
                                             'http://www.redirect.mysite.com/?id=');
    });
    

    【讨论】:

    • 不是我想要的,但只有很少的配置
    • $('iframe').each(function(){ this.src = this.src.replace('http://www.youtube.com/embed/', 'http://www.redirect.mysite.com/?id='); });
    • @Yusaf 现在应该由 snuf 决定:-D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-02
    • 2014-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-19
    相关资源
    最近更新 更多