英文:
Change the link inside of an embed on each matching anchor link
问题
我正在使用一个非常有限的系统,不允许直接编辑HTML,所以我只能通过Javascript / JQuery来添加任何内容(我在这方面还很新手)。
我尝试的操作如下:
- 识别具有匹配模式的链接
- 存储其href值
- 注入HTML代码以显示iframe
- 将iframe引用的视频href替换为先前存储的href
- 针对页面上的每个匹配的href进行循环
到目前为止,我已经成功使iframe显示出正确的视频,但是,如果我添加多个匹配的链接 - 但是它们指向不同的视频/hrefs - 结果是重复嵌入的视频完全相同。
以下是我目前的代码:
$('a[href*="https://app.application.io/recordings/"]').each(function(index,item){
var applicationEmbed = $(this).attr("href");
var applicationiFrame = '<div class="video-embed" style="position: relative; padding-bottom: 50.5%; height: 0;"><iframe src="' + applicationEmbed + '/embed" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;"></iframe></div>';
$("a[href*='https://app.application.io/recordings/']").replaceWith(applicationiFrame);
});
有什么建议吗?
英文:
I'm working with a very limited system that doesn't allow for direct HTML editing so anything I want to add is only capable through Javascript / JQuery (which I'm still very new at).
What I'm trying to do is as follows:
- Identify links with a matching pattern
- Have its href value stored
- Inject HTML code to display an iframe
- Replace the video href the iframe references to the href stored earlier
- Loop for every matching href on the page
So far, I've been able to get the iframe to display with the correct video, however, if I add multiple matching links - but to different videos/hrefs - the result is the exact same embedded video repeatedly.
Here's my code so far:
$('a[href*=\"https://app.application.io/recordings/\"]').each(function(index,item){
var applicationEmbed = $(this).attr("href");
var applicationiFrame = '<div class=\"video-embed\" style=\"position: relative; padding-bottom: 50.5%; height: 0;\"><iframe src=\"' + applicationEmbed + '/embed\" frameborder=\"0\" webkitallowfullscreen mozallowfullscreen allowfullscreen style=\"position: absolute; top: 0; left: 0; width: 100%; height: 100%;\"></iframe></div>'
$("a[href*=\"https://app.application.io/recordings/\"]").replaceWith(applicationiFrame);
});
Any tips?
答案1
得分: 0
这是翻译好的部分:
这是因为你的最后一行告诉脚本用单一的值 replaceWith(applicationiFrame)
替换每个链接模式 $("a[href*=\"https://app.application.io/recordings/\"]")
的每个实例,这使它们都变得相同。这里是另一种方法,注意我如何使用模板字面量来提高可读性。
$('a[href*="https://app.application.io/recordings/"]').each(function(index, item) {
let applicationEmbed = $(item).attr("href");
let applicationiFrame = `
<div class="video-embed" style="position: relative; padding-bottom: 50.5%; height: 0;">
<iframe src="${applicationEmbed}/embed" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;"></iframe>
</div>`
$(item).before(applicationiFrame).remove()
});
.video-embed {
border: 1px solid #666;
background: #dedede;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="https://app.application.io/recordings/1">Test link</a><br />
<a href="https://app.application.io/recordings/2">Test link</a><br />
<a href="https://app.application.io/recordings/3">Test link</a><br />
<a href="https://app.application.io/recordings/4">Test link</a><br />
英文:
It's because your last line is telling the script to replace every instance of the link pattern $("a[href*=\"https://app.application.io/recordings/\"]")
with the single value replaceWith(applicationiFrame)
, which is making them all the same. Here's an alternate approach an note how I used template literals to help with readability.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$('a[href*=\"https://app.application.io/recordings/\"]').each(function(index, item) {
let applicationEmbed = $(item).attr("href");
let applicationiFrame = `
<div class="video-embed" style="position: relative; padding-bottom: 50.5%; height: 0;">
<iframe src="${applicationEmbed}/embed" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;"></iframe>
</div>`
$(item).before(applicationiFrame).remove()
});
<!-- language: lang-css -->
.video-embed {
border: 1px solid #666;
background: #dedede;
}
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="https://app.application.io/recordings/1">Test link</a><br />
<a href="https://app.application.io/recordings/2">Test link</a><br />
<a href="https://app.application.io/recordings/3">Test link</a><br />
<a href="https://app.application.io/recordings/4">Test link</a><br />
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论