开发者

jQuery: Selecting the currently selected element (example)

For example, I have something like:

<div data-url="http://google.com">
  <iframe src=""></iframe>
</div>
<div data-url="开发者_StackOverflow社区http://facebook.com">
  <iframe src=""></iframe>
</div>
<div data-url="http://youtube.com">
  <iframe src=""></iframe>
</div>

<script>
$('div iframe').attr('src',$(this).parent().attr('data-url'));
</script>

I want to delay the loading of the iframe using jQuery. I want to set the iframe's "src" to be the same as its parent's "data-url". How can I do that?

Or, is there another way to delay the loading of an iframe until everything else loads?


In your code this is actually the window object. You want to wrap this code in a each method. (Also, notice how this code doesn't call $(this) twice)

$('div iframe').each(function(ev){
    var elem = $(this);
    elem.attr('src', elem.parent().data('url')); 
});


Here's how you add the data-url to the src:

 $('div').each(function(){
        $(this).children('iframe').attr('src',$(this).attr('data-url'));
    });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜