【问题标题】:jquery function replaces all iframes with the same thingjquery 函数用相同的东西替换所有 iframe
【发布时间】:2011-11-20 21:37:59
【问题描述】:

我的页面上有多个 iframe,这个 jQuery 将它们全部替换为 example here http://jsfiddle.net/yusaf/g6Mdq/4/,因为您将看到两者的 iframe src 不同 我试图实现.each,但我没有成功:/

<script>
$(function (){
    if($(".vid-wrap iframe").length){
        var youtubeid = $(".vid-wrap iframe").attr("src").match(/[\w\-]{11,}/)[0];

        $(".vid-wrap iframe").replaceWith('<a onclick="window.open(&#39;http://www.youtube.com/watch_popup?v=' + youtubeid +'&vq=medium&#39;,&#39;Video&#39;,&#39;width=560, height=315&#39;); return false;"><img style="width:480px;height:360px;background-image:url(http://i1.ytimg.com/vi/' + youtubeid +'/hqdefault.jpg);" class="someClass" src="http://3.bp.blogspot.com/--0FurTdrYzg/Tsj3RPXGz4I/AAAAAAAAAGI/mm_4UvmoD-A/s1600/standard.png"/></a>');
    }
});
</script>

【问题讨论】:

  • 你有没有想过把你的代码写在一行上?
  • @Eric 试试 jsfiddle,如果有帮助的话
  • 那同样没用。 Smamatti 现已修复。
  • Esailja 不,它不是我在使用 jquery 时遇到的问题,我想出了一个替代解决方案,它无法工作,因为它取代了所有 iframe,它是该问题的答案,但它有一个问题,可能重复?!!!

标签: jquery


【解决方案1】:

这将起作用:

$(function (){
    $(".vid-wrap iframe").each(function() {
        var youtubeid = $(this).attr("src").match(/[\w\-]{11,}/)[0];

        $(this).replaceWith(
            $('<a />').click(function() {
                 window.open(
                     'http://www.youtube.com/watch_popup?v=' + youtubeid +'&vq=medium',
                     'Video',
                     'width=560, height=315'
                 );
            }).append(
                '<img style="width:480px;height:360px;background-image:url(http://i1.ytimg.com/vi/' + youtubeid +'/hqdefault.jpg);" class="someClass" src="http://3.bp.blogspot.com/--0FurTdrYzg/Tsj3RPXGz4I/AAAAAAAAAGI/mm_4UvmoD-A/s1600/standard.png"/>'
            )
        );
    });
});

【讨论】:

  • @Yusaf:添加缺少的括号超出了您的能力范围吗?
  • 我写jquery的时间不长,对我来说很难,而且你最近的编辑没有奏效
  • @Yusaf:那是因为javascript使用.vid-wrap,而你的HTML使用video-wrapIt works after that is fixed
  • 感谢您的贡献,这也是一个措辞非常糟糕的问题,谢谢
【解决方案2】:

【讨论】:

  • 但我所做的只是获取 src 并使用它来超链接图像。
  • 通过 html 字符串添加 onclick 处理程序是一种糟糕的做法!
猜你喜欢
  • 2012-07-21
  • 1970-01-01
  • 1970-01-01
  • 2013-11-12
  • 2016-07-03
  • 1970-01-01
  • 1970-01-01
  • 2017-01-29
  • 1970-01-01
相关资源
最近更新 更多