更改嵌入中每个匹配锚链接的链接。

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

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:

  $(&#39;a[href*=\&quot;https://app.application.io/recordings/\&quot;]&#39;).each(function(index,item){
  var applicationEmbed = $(this).attr(&quot;href&quot;);
  var applicationiFrame = &#39;&lt;div class=\&quot;video-embed\&quot; style=\&quot;position: relative; padding-bottom: 50.5%; height: 0;\&quot;&gt;&lt;iframe src=\&quot;&#39; + applicationEmbed + &#39;/embed\&quot; frameborder=\&quot;0\&quot; webkitallowfullscreen mozallowfullscreen allowfullscreen style=\&quot;position: absolute; top: 0; left: 0; width: 100%; height: 100%;\&quot;&gt;&lt;/iframe&gt;&lt;/div&gt;&#39;
    $(&quot;a[href*=\&quot;https://app.application.io/recordings/\&quot;]&quot;).replaceWith(applicationiFrame);
    });

Any tips?

答案1

得分: 0

这是翻译好的部分:

这是因为你的最后一行告诉脚本用单一的值 replaceWith(applicationiFrame) 替换每个链接模式 $(&quot;a[href*=\&quot;https://app.application.io/recordings/\&quot;]&quot;) 的每个实例,这使它们都变得相同。这里是另一种方法,注意我如何使用模板字面量来提高可读性。

$('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 $(&quot;a[href*=\&quot;https://app.application.io/recordings/\&quot;]&quot;) 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 -->

$(&#39;a[href*=\&quot;https://app.application.io/recordings/\&quot;]&#39;).each(function(index, item) {
  let applicationEmbed = $(item).attr(&quot;href&quot;);
  let applicationiFrame = `
  &lt;div class=&quot;video-embed&quot; style=&quot;position: relative; padding-bottom: 50.5%; height: 0;&quot;&gt;
  &lt;iframe src=&quot;${applicationEmbed}/embed&quot; frameborder=&quot;0&quot; webkitallowfullscreen mozallowfullscreen allowfullscreen style=&quot;position: absolute; top: 0; left: 0; width: 100%; height: 100%;&quot;&gt;&lt;/iframe&gt;
  &lt;/div&gt;`
  $(item).before(applicationiFrame).remove()
});

<!-- language: lang-css -->

.video-embed {
  border: 1px solid #666;
  background: #dedede;
}

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

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;a href=&quot;https://app.application.io/recordings/1&quot;&gt;Test link&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;https://app.application.io/recordings/2&quot;&gt;Test link&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;https://app.application.io/recordings/3&quot;&gt;Test link&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;https://app.application.io/recordings/4&quot;&gt;Test link&lt;/a&gt;&lt;br /&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月18日 01:04:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76274553.html
匿名

发表评论

匿名网友

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

确定