将属性从一个元素填充到每个函数中的另一个元素

时间:2017-03-11 22:43:18

标签: javascript jquery iframe

如何使用a元素中的href属性填充iframe src属性?如果需要,可以在li.feed-item元素内移动iframe。

我需要像每个函数一样的东西,因为有几个li元素具有不同的链接。

我得到的最近的是这个,但它会获取所有iframe的第一个链接。

jQuery(document).ready(function () {
jQuery('li.feed-item').each(function () {         
  var href = jQuery("li.feed-item a.colorbox.cboxElement").attr('href');
jQuery('li.feed-item a.colorbox.cboxElement iframe').attr('src', href);
});
});

HTML

<ul>
    <li class="feed-item">
        <a class="colorbox cboxElement" href="http://www.youtube.com/embed/Tp-VAe59RyA"><img height="280" src="http://skateflix.se/wp-content/uploads/cache/remote/i1-ytimg-com/1384690600.jpg" width="280">
         <iframe allowfullscreen class="video-embed" height="100%" itemprop="video" src="http://www.youtube.com/embed/werwerw4wer" width="100%">
         </iframe></a>
    </li>
</ul>

<ul>
    <li class="feed-item">
        <a class="colorbox cboxElement" href="http://www.youtube.com/embed/Tp-VAe59RyA"><img height="280" src="http://skateflix.se/wp-content/uploads/cache/remote/i1-ytimg-com/1384690600.jpg" width="280">
         <iframe allowfullscreen class="video-embed" height="100%" itemprop="video" src="http://www.youtube.com/embed/Qq0u6vbzXXoeerer" width="100%">
         </iframe></a>
    </li>
</ul>

2 个答案:

答案 0 :(得分:2)

基本上始终参考brew link rabbitmq,即当前迭代的$(this)

li.feed-item

甚至更简单,首先查询您的jQuery(function($) { // DOM ready and $ alias secured $('li.feed-item').each(function() { var href = $(this).find("a.cboxElement").attr('href'); $(this).find("iframe").attr('src', href); }); }); ,然后查找iframe父级:

a

答案 1 :(得分:0)

使用&#39;这个&#39;应该工作:

jQuery(document).ready(function () {
    jQuery('li.feed-item').each(function () {  
      var href = jQuery(this).find("a.colorbox.cboxElement").attr('href');
    jQuery(this).attr('src', href);
    });
    });
相关问题