更改iFrame的src是使用jQuery删除iFrame

时间:2011-10-27 22:31:44

标签: jquery iframe

使用这个jQuery:

$('#mycarousel li img').click(function(){
    $('#video').empty();
    var newId = $(this).attr('id');
    newId = newId.replace('vid','');
    $('#video iframe').attr('src', $('.vidHolder' + newId).html());
});

相关的HTML:

<ul id="mycarousel" class="jcarousel-skin-tango"><li><img src="pathtoimage" id="vid1" width="90" height="55" alt="" /></li>

<div id="video"><iframe src="pathtocurrentiframe" style="overflow:hidden"  id="fr"  width="425" height="480" frameborder="0" hspace="0" vspace="0" style="overflow:hidden;"></iframe><div id="dialog" title="" style="width: 0; height: 0;"></div></div>

<div class="vidHolder1" style="display: none;">newpathtoiframe</div>

现在由于某种原因,当我点击img它只是删除iFrame!没有像我需要的那样改变src ......

1 个答案:

答案 0 :(得分:0)

我猜你的问题是点击功能的第一行:

$('#video').empty();

.empty()方法会删除所有子元素,这意味着它会删除您的iframe。

你函数中的最后一行什么都不做,因为没有元素匹配选择器(但你不会得到错误,因为当没有元素与选择器匹配时,jQuery很乐意不做任何事情。)

相关问题