获取所有子视频封面值,并使用相应父视频HTML标记更新值。

huangapple go评论71阅读模式
英文:

JQuery - Get all child video poster value and update value with respective parent video html tag

问题

尝试更新每个视频的HTML标记(类名=".video_block video")的属性值,使用它们各自的子div标记(类名=".posterURL")。

JQuery -

$(document).ready(function() {
    $('.video_block .posterURL').each(function() {
        var poster = $(this).data("poster");
        $(this).closest('.video_block').find('video').attr('poster', poster);
    });
});

HTML -

<div class="container">
    <div class="media_block">

        <div class="video_block">
            <video
            controls
            src="https://archive.org/download/BigBuckBunny_124/Content/big_buck_bunny_720p_surround.mp4"
            width="620">
            </video>

            <div class="posterURL" data-poster="https://peach.blender.org/wp-content/uploads/title_anouncement.jpg?x11217"></div>
        </div>

        <div class="video_block">
            <video
            controls
            src="https://archive.org/download/BigBuckBunny_124/Content/big_buck_bunny_720p_surround.mp4"
            width="620">
            </video>

            <div class="posterURL" data-poster="https://peach.blender.org/wp-content/uploads/title_anouncement.jpg?x11217"></div>
        </div>

        <div class="video_block">
            <video
            controls
            src="https://archive.org/download/BigBuckBunny_124/Content/big_buck_bunny_720p_surround.mp4"
            width="620">
            </video>

            <div class="posterURL" data-poster="https://peach.blender.org/wp-content/uploads/title_anouncement.jpg?x11217"></div>
        </div>
    </div>
</div>

请分享您的想法!

提前感谢!

英文:

Trying to update the attribute value for each video html tag (classname=".video_block video") by using their respective child div tag (classname="posterURL").

JQuery -

$(document(){
    $(&#39;.video_block .posterURL&#39;).each(function() {
        var poster = $(this).data(&quot;poster&quot;)
        $(.video_block video).attr(&#39;poster&#39;, poster);
    })
})

HTML -


&lt;div class=&quot;container&quot;&gt;
    &lt;div class=&quot;media_block&quot;&gt;

        &lt;div class=&quot;video_block&quot;&gt;
            &lt;video
            controls
            src=&quot;https://archive.org/download/BigBuckBunny_124/Content/big_buck_bunny_720p_surround.mp4&quot;
            width=&quot;620&quot;&gt;
            &lt;/video&gt;

            &lt;div class=&quot;posterURL&quot; data-poster=&quot;https://peach.blender.org/wp-content/uploads/title_anouncement.jpg?x11217&quot;&gt;&lt;/div&gt;&lt;/div&gt;
        &lt;/div&gt;

        &lt;div class=&quot;video_block&quot;&gt;
            &lt;video
            controls
            src=&quot;https://archive.org/download/BigBuckBunny_124/Content/big_buck_bunny_720p_surround.mp4&quot;
            width=&quot;620&quot;&gt;
            &lt;/video&gt;

            &lt;div class=&quot;posterURL&quot; data-poster=&quot;https://peach.blender.org/wp-content/uploads/title_anouncement.jpg?x11217&quot;&gt;&lt;/div&gt;&lt;/div&gt;
        &lt;/div&gt;

        &lt;div class=&quot;video_block&quot;&gt;
            &lt;video
            controls
            src=&quot;https://archive.org/download/BigBuckBunny_124/Content/big_buck_bunny_720p_surround.mp4&quot;
            width=&quot;620&quot;&gt;
            &lt;/video&gt;

            &lt;div class=&quot;posterURL&quot; data-poster=&quot;https://peach.blender.org/wp-content/uploads/title_anouncement.jpg?x11217&quot;&gt;&lt;/div&gt;&lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

Please share your thoughts!

Thanks in advance!

答案1

得分: 1

你有一些语法错误,需要遍历从.posterURL元素到其兄弟元素以定位适当的video元素。

整个过程可以简化如下:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->
$(".video_block video").attr("poster", function() {
  return $(this).next(".posterURL").data("poster");
});

<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.slim.min.js"></script>
<div class="video_block">
  <video controls src="https://archive.org/download/BigBuckBunny_124/Content/big_buck_bunny_720p_surround.mp4" width="620"></video>

  <div class="posterURL" data-poster="https://peach.blender.org/wp-content/uploads/title_anouncement.jpg?x11217"></div>
</div>

<div class="video_block">
  <video controls src="https://archive.org/download/BigBuckBunny_124/Content/big_buck_bunny_720p_surround.mp4" width="620"></video>

  <div class="posterURL" data-poster="https://peach.blender.org/wp-content/uploads/title_anouncement.jpg?x11217"></div>
</div>

<div class="video_block">
  <video controls src="https://archive.org/download/BigBuckBunny_124/Content/big_buck_bunny_720p_surround.mp4" width="620"></video>

  <div class="posterURL" data-poster="https://peach.blender.org/wp-content/uploads/title_anouncement.jpg?x11217"></div>
</div>

<!-- end snippet -->

即,对于每个<video>元素,将其poster属性值设置为下一个.posterURL兄弟元素的data-poster值。

英文:

You have a couple of syntax errors and will need to traverse from the .posterURL element to its sibling to target the appropriate video element.

The whole thing can be simplified like this

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&quot;.video_block video&quot;).attr(&quot;poster&quot;, function() {
  return $(this).next(&quot;.posterURL&quot;).data(&quot;poster&quot;);
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.slim.min.js&quot;&gt;&lt;/script&gt;
&lt;div class=&quot;video_block&quot;&gt;
  &lt;video controls src=&quot;https://archive.org/download/BigBuckBunny_124/Content/big_buck_bunny_720p_surround.mp4&quot; width=&quot;620&quot;&gt;&lt;/video&gt;

  &lt;div class=&quot;posterURL&quot; data-poster=&quot;https://peach.blender.org/wp-content/uploads/title_anouncement.jpg?x11217&quot;&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;div class=&quot;video_block&quot;&gt;
  &lt;video controls src=&quot;https://archive.org/download/BigBuckBunny_124/Content/big_buck_bunny_720p_surround.mp4&quot; width=&quot;620&quot;&gt;&lt;/video&gt;

  &lt;div class=&quot;posterURL&quot; data-poster=&quot;https://peach.blender.org/wp-content/uploads/title_anouncement.jpg?x11217&quot;&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;div class=&quot;video_block&quot;&gt;
  &lt;video controls src=&quot;https://archive.org/download/BigBuckBunny_124/Content/big_buck_bunny_720p_surround.mp4&quot; width=&quot;620&quot;&gt;&lt;/video&gt;

  &lt;div class=&quot;posterURL&quot; data-poster=&quot;https://peach.blender.org/wp-content/uploads/title_anouncement.jpg?x11217&quot;&gt;&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

That is, for each &lt;video&gt; element, set its poster attribute value to the data-poster value of the next .posterURL sibling element.

huangapple
  • 本文由 发表于 2023年7月13日 14:26:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76676486.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定