我正在尝试从 iframe 嵌入代码中获取 youtube src 并将其作为变量传递。
例如,如果我的 YouTube 嵌入代码是:
<iframe width="1024" height="576" src="https://www.youtube.com/embed/rfxESg1_hEA?feature=oembed"frameborder="0"allowfullscreen></iframe>
我正在尝试获取 https://www.youtube.com/embed/rfxESg1_hEA
有没有办法用 jQuery 做到这一点?
最佳答案
试试这个:
var src = $("iframe").attr("src").split('?')[0];
alert(src);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<iframe width="1024" height="576" src="https://www.youtube.com/embed/rfxESg1_hEA?feature=oembed"frameborder="0"allowfullscreen"></iframe>
关于jQuery 从 iframe 代码获取 YouTube 视频 src,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32825871/