jQuery 从 iframe 代码获取 YouTube 视频源

jQuery get YouTube video src from iframe code

我正在尝试从 iframe 嵌入代码中获取 youtube src 并将其作为变量传递。

例如,如果我的 YouTube 嵌入代码是:

<iframe width=&quot;1024&quot; height=&quot;576&quot; src=&quot;https://www.youtube.com/embed/rfxESg1_hEA?feature=oembed&quot;frameborder=&quot;0&quot;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=&quot;1024&quot; height=&quot;576&quot; src="https://www.youtube.com/embed/rfxESg1_hEA?feature=oembed&quot;frameborder=&quot;0&quot;allowfullscreen"></iframe>