【问题标题】:Auto Embedd Youtube Videos [closed]自动嵌入 Youtube 视频 [关闭]
【发布时间】:2013-12-04 08:35:23
【问题描述】:

我有如下要求:

YouTube 链接应在发布 youtube 网址时自动嵌入帖子中

谁能给我一个开场白?

【问题讨论】:

  • 谷歌嵌入你的管元素?

标签: jquery youtube


【解决方案1】:

我已经更改了多个网址,试试这个

</head>
<body>
    <div class="condent">Lorem Ipsum is simply dummy text of the printing and http://www.youtube.com/watch?v=8YUmZNJUAHk No typesetting http://www.youtube.com/watch?v=8YUmZNJUAHk industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

    <script type="text/javascript">
    $(document).ready(function(){
        var textContent = $('.condent').text();
        var str1 = textContent.split(' ');
        for (var i = 0, il = str1.length; i < il; i++) {
            if (str1[i].indexOf("http://www.youtube.com/watch?v=") >= 0){
                var youtubeURL = str1[i];
                var youtubeId = youtubeURL.replace('http://www.youtube.com/watch?v=',"");
                var embedCode = '<iframe width="560" height="315" src="//www.youtube.com/embed/'+youtubeId+'" frameborder="0" allowfullscreen></iframe>';
                textContent = textContent.replace(youtubeURL,embedCode);

            }
        }
        $('.condent').html(textContent);
    });
    </script>
</body>

【讨论】:

  • 上述代码的问题是,当我放2个或更多youtube链接时,它会中断,如果在URL中添加了一些额外的参数,它也会中断
  • 我已将代码从单个 url 更改为多个 url.. 参见上面的代码
猜你喜欢
  • 2016-03-14
  • 2015-06-09
  • 2016-11-19
  • 2022-01-17
  • 2018-10-07
  • 2011-04-24
  • 2017-11-03
  • 2016-06-19
  • 2018-12-16
相关资源
最近更新 更多