【发布时间】:2021-03-28 21:06:42
【问题描述】:
我有两个脚本,一个将链接转换为 youtube 播放器,它适用于 ajax,现在我想做同样的事情,但使用 img 链接将其转换为 img 标签,而这个不适用于 ajax,我的意思是 ajax 在论坛线程和寻呼机是 ajax 如果你转到下一页它不再工作你必须刷新窗口。
<script>
$(document).ready(function () {
var r = /^((?:https?:)?\/\/)?((?:www|m)\.)?((?:youtube\.com|youtu.be))(\/(?:[\w\-]+\?v=|embed\/|v\/)?)((?!\bchannel\b)[\w\-]+)([\S]+)?$/;
function c() {
$("#content-content a").each(function () {
var e,
o,
t = $(this).attr("href"),
c = r.exec(t);
null !== c &&
(console.log(c[5], c[6]),
(e = c[5]),
void 0 !== c[6] && c[6].toLowerCase().includes("t=") && (e += c[6]),
console.log(e),
(o = '<iframe width="100%" height="315" src="https://www.youtube.com/embed/{youtubeVideoId}" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>'.replace(
"{youtubeVideoId}",
e
)),
$(this).replaceWith(o));
});
}
c(),
(Drupal.behaviors.YtUrlToPlayerBehaviour = function (e, o) {
$(document).ajaxComplete(function (e, o, t) {
c();
});
});
});
</script>
<script>
$('a[href$=".png"], a[href$=".jpg"], a[href$=".gif"], a[href$=".tiff"], a[href$=".webp"]').each(function(){
$(this).html('<img src="' + $(this).attr('href') + '" />');
});
</script>
【问题讨论】:
标签: javascript jquery ajax drupal