javascript 通过 jquery 将 data-src 更改为 src
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24414488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change data-src to src via jquery
提问by RMH
Im using a flexslider that has audio files for each slide, but I don't want to load countless audio files right away on the page. So im trying to get the data-src to become the src after each slide
我使用的 flexslider 为每张幻灯片都提供了音频文件,但我不想立即在页面上加载无数的音频文件。所以我试图在每张幻灯片之后让 data-src 成为 src
the slides are currently as follows:
目前的幻灯片如下:
<div class="flexslider carousel">
<ul class="slides">
<li>
<img src="http://www.quinnmedical.com/skin/frontend/gigasavvy/quinn/ppt/Slide01.jpg" />
<audio id="demo" controls>
<source src="/skin/frontend/gigasavvy/quinn/audio/Slide1.mp3" type="audio/mpeg" />
</audio>
</li>
<li>
<img src="http://www.quinnmedical.com/skin/frontend/gigasavvy/quinn/ppt/Slide03.jpg" />
<audio id="demo" controls>
<source data-src="/skin/frontend/gigasavvy/quinn/audio/Slide3.mp3" src="" type="audio/mpeg" />
</audio>
</li>
</ul>
</div>
In the after function i want to change data-src to src. Any help would be greatly appreciated as how to go from data-src to src
在 after 函数中,我想将 data-src 更改为 src。关于如何从 data-src 到 src 的任何帮助将不胜感激
采纳答案by dvk317960
Renaming an attribute will not be possible. you can create new attributes and remove the old attribute. Suppose if there is a click event, it will be possible as as below. Please movdiy the event according to your requirement.
无法重命名属性。您可以创建新属性并删除旧属性。假设如果有点击事件,则可能如下。请根据您的要求移动事件。
$('#demo').on("click", "img", function () {
var t = this;
var source = $(t).children("source");
$source.attr({
src: $t.attr('data-src')
}).removeAttr('data-src');
}
回答by Raenix
Try this:
试试这个:
$('source').attr("src", $(this).data('src'));
$('source').removeAttr('data-src');
This should move the url from data-srcto src, and then remove the data-srcattribute.
这应该将 url 从 移动data-src到src,然后删除该data-src属性。
回答by Adam Merrifield
Inside the after function get the currently visible slide and then get the auto on it and it's source. Next check to see if it already has a srcattribute. If not then set it to it's own .data('src'). Add this into the flexslider object.
在 after 函数中获取当前可见的幻灯片,然后在其上获取自动及其源。接下来检查它是否已经有一个src属性。如果没有,则将其设置为它自己的.data('src')。将此添加到 flexslider 对象中。
after: function(){
var $currentAudio = $('.flex-active-slide audio source');
if(!$currentAudio.attr('src')){
$currentAudio.attr('src', $currentAudio.data('src'));
}
}

