如何解决使 img 嵌入脚本与 drupal ajax 一起工作
我有这个脚本可以在带有 jquery 1.2.6 的 drupal 6 中与 ajax pager 一起使用,它将 youtube 链接转换为嵌入,现在尝试使用图像链接执行相同操作,但它不起作用我每次都必须刷新页面。
$(document).ready(function(){
var ytRegExpUrl = /^((?:https?:)?\/\/)?((?:www|m)\.)?((?:youtube\.com|youtu.be))(\/(?:[\w\-]+\?v=|embed\/|v\/)?)((?!\bchannel\b)[\w\-]+)([\S]+)?$/;
var ytFrame = '<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>'
function replaceUrlToPlayer() {
$("#content-content a").each(function() {
var toText = $(this).attr('href');
var match = ytRegExpUrl.exec(toText);
if (match !== null) {
var textToReplace = match[5];
if(typeof(match[6]) != "undefined"){
if(match[6].toLowerCase().includes("t=")){
textToReplace += match[6];
}
}
var replacement = ytFrame.replace('{youtubeVideoId}',textToReplace);
$(this).replaceWith(replacement);
}
});
};
Drupal.behaviors.YtUrlToPlayerBehaviour = function(context,settings) {
$(document).ajaxComplete(function(event,xhr,settings) {
replaceUrlToPlayer();
});
}
});
img 嵌入脚本不起作用
$('a[href$=".png"],a[href$=".jpg"],a[href$=".gif"],a[href$=".tiff"],a[href$=".webp"]').each(function(){
$(this).html('<img src="' + $(this).attr('href') + '" />');
});
我尝试过,但没有成功
$(document).ready(function(){
var ytRegExpUrl = /^(http(s?):)([/|.|\w|\s|-])*\.(?:jpg|gif|png)?$/;
var imgFrame = '<img src="' + $(this).attr('href') + '" />'
function replaceUrlToImg() {
$("#content-content a").each(function() {
var toText = $(this).attr('href');
var match = ytRegExpUrl.exec(toText);
if (match !== null) {
var textToReplace = match[5];
if(typeof(match[6]) != "undefined"){
if(match[6].toLowerCase().includes("t=")){
textToReplace += match[6];
}
}
var replacement = ytFrame.replace('<img src="' + $(this).attr('href') + '" />',textToReplace);
$(this).replaceWith(replacement);
}
});
};
Drupal.behaviors.YtUrlToImgBehaviour = function(context,settings) {
replaceUrlToImg();
});
}
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。