如何在youtube插件中将src更改为data-src?

时间:2017-07-23 09:03:01

标签: javascript src

我想通过javascript代码添加data-src而不是SRC。请帮帮我,我花了很多时间。它适用于Lazy加载概念。



<iframe id="mbYTP_ut-background-video-hero" class="playerBox" style="position: absolute; z-index: 0; width: 1772.76px; height: 859px; top: 0px; left: 0px; overflow: hidden; opacity: 1; margin-top: -330px; margin-left: -211.88px; transition-property: opacity; transition-duration: 1000ms;" frameborder="0" allowfullscreen="1" title="YouTube video player" width="640" height="360" src="https://www.youtube.com/embed/KhzGSHNhnbI"></iframe>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您可以使用jQuery来完成。根据您的准则,下面给出了示例。
首先取 src 值。然后添加新属性 data-src 并将 src 值保留在 data-src 中。然后,如果需要,您可以删除 src
jQuery部分:

      <div>John Resig</div>
        <div>George Martin</div>
       <div>Malcom John Sinclair</div>
        <div>J. Ohn</div>

      <script>
       $( "div:contains('John')" ).css( "text-decoration", "underline" );
        </script>
$(document).ready(function(){
        /*Get value from src*/
        var get_src = $('#mbYTP_ut-background-video-hero').attr('src');

        /*Add new attribute data-src and put src value here*/
	$('#mbYTP_ut-background-video-hero').attr('data-src',get_src);

       /*Remove src here. If you don't need to remove src then comment below part */
	$('#mbYTP_ut-background-video-hero').removeAttr('src');
});