检查iframe内是否存在动态属性值

Check if dynamic attribute value exist inside iframe

本文关键字:动态 属性 存在 是否 iframe 检查      更新时间:2023-09-26

所以我有这个iframe更新"data-active-video"属性的值每次用户点击下一步在幻灯片。我试图检查是否"data-active-video"的值为4,如果它确实改变了"src"的值..见下面的代码thx

<div class="video-frame">
    <iframe width="560" height="315" src="EZF4iS-XEQ8" frameborder="0" id="active_video_player" allowfullscreen="" data-active-video="4"></iframe>
</div>

和JS:

$('.video-frame').find('iframe').each(function () {
    if ($(this).attr('data-active-video', '4')) {
        // alert('yesssss');
        $(this).attr('src', 'http://www.somelink.com');
    }
});

或者您也可以这样尝试。因为看起来你正在使用jQuery。您可以使用data属性。这两种方法都和我上面的答案一样。

$('.video-frame').find('iframe').each(function () {
if ($(this).data('active-video') == '4') {
    // alert('yesssss');
    $(this).attr('src', 'http://www.somelink.com');
}

});

attr('data-active-video', '4') 将属性值设置为4,要获取数据属性值需要使用 attr('data-active-video') data('active-video')

$('.video-frame iframe').each(function () {
    if ($(this).attr('data-active-video') == '4') {
        // alert('yesssss');
        $(this).attr('src', 'http://www.somelink.com');
    }
});