gpt4 book ai didi

jquery - 使用 AJAX 加载 jQuery

转载 作者:行者123 更新时间:2023-12-01 04:44:26 25 4
gpt4 key购买 nike

正如我们大多数人所知,jQuery 在页面加载时加载/“激活”,但如果您通过 AJAX 代码引入内容,则在大多数情况下需要很少的修改或重新启动。

我想让 Magnific 弹出窗口中的 FitVids 能够在 AJAX 加载的内容中工作。当我成功修复 Bootstrap 工具提示和其他一些小问题时,所有尝试都失败了。

<小时/>

ma​​in.js 内的 Fitvids

(function( $ ) {

"use strict";

$.fn.fitVids = function( options ) {
var settings = {
customSelector: null,
ignore: null,
};

if(!document.getElementById('fit-vids-style')) {
// appendStyles: https://github.com/toddmotto/fluidvids/blob/master/dist/fluidvids.js
var head = document.head || document.getElementsByTagName('head')[0];
var css = '.fluid-width-video-wrapper{width:100%;position:relative;padding:0;}.fluid-width-video-wrapper iframe,.fluid-width-video-wrapper object,.fluid-width-video-wrapper embed {position:absolute;top:0;left:0;width:100%;height:100%;}';
var div = document.createElement('div');
div.innerHTML = '<p>x</p><style id="fit-vids-style">' + css + '</style>';
head.appendChild(div.childNodes[1]);
}

if ( options ) {
$.extend( settings, options );
}

return this.each(function(){
var selectors = [
"iframe[src*='player.vimeo.com']",
"iframe[src*='youtube.com']",
"iframe[src*='youtube-nocookie.com']",
"iframe[src*='kickstarter.com'][src*='video.html']",
"object",
"embed"
];

if (settings.customSelector) {
selectors.push(settings.customSelector);
}

var ignoreList = '.fitvidsignore';

if(settings.ignore) {
ignoreList = ignoreList + ', ' + settings.ignore;
}

var $allVideos = $(this).find(selectors.join(','));
$allVideos = $allVideos.not("object object"); // SwfObj conflict patch
$allVideos = $allVideos.not(ignoreList); // Disable FitVids on this video.

$allVideos.each(function(){
var $this = $(this);
if($this.parents(ignoreList).length > 0) {
return; // Disable FitVids on this video.
}
if (this.tagName.toLowerCase() === 'embed' && $this.parent('object').length || $this.parent('.fluid-width-video-wrapper').length) { return; }
if ((!$this.css('height') && !$this.css('width')) && (isNaN($this.attr('height')) || isNaN($this.attr('width'))))
{
$this.attr('height', 9);
$this.attr('width', 16);
}
var height = ( this.tagName.toLowerCase() === 'object' || ($this.attr('height') && !isNaN(parseInt($this.attr('height'), 10))) ) ? parseInt($this.attr('height'), 10) : $this.height(),
width = !isNaN(parseInt($this.attr('width'), 10)) ? parseInt($this.attr('width'), 10) : $this.width(),
aspectRatio = height / width;
if(!$this.attr('id')){
var videoID = 'fitvid' + Math.floor(Math.random()*999999);
$this.attr('id', videoID);
}
$this.wrap('<div class="fluid-width-video-wrapper"></div>').parent('.fluid-width-video-wrapper').css('padding-top', (aspectRatio * 100)+"%");
$this.removeAttr('height').removeAttr('width');
});
});
};
// Works with either jQuery or Zepto
})( window.jQuery || window.Zepto );
;

ma​​in.js 中的一些额外 Fitvids + 弹出窗口

jQuery(document).find('.property-video-popup').magnificPopup({
type: 'iframe'
});

触发弹出窗口的 HTML

<a href=" video URL " class="property-video-popup"> Icon, text, tooltip etc </a>

看来我对弹出窗口的看法是错误的 - 它是单独的 min.js 文件。我设法将其缩小,但我应该警告你 - 它相当长。有字符限制,我被迫删掉一些。如果有重要的内容丢失,请告诉我!

(function(e) {
var t, n, i, o, r, a, s, l = "Close",
c = "BeforeClose",
d = "AfterClose",
u = "BeforeAppend",
p = "MarkupParse",
f = "Open",
m = "Change",
g = "mfp",
h = "." + g,
v = "mfp-ready",
C = "mfp-removing",
y = "mfp-prevent-close",
w = function() {}, b = !! window.jQuery,
I = e(window),
x = function(e, n) {
t.ev.on(g + e + h, n)
}, k = function(t, n, i, o) {
var r = document.createElement("div");
return r.className = "mfp-" + t, i && (r.innerHTML = i), o ? n && n.appendChild(r) : (r = e(r), n && r.appendTo(n)), r
}, T = function(n, i) {
t.ev.triggerHandler(g + n, i), t.st.callbacks && (n = n.charAt(0).toLowerCase() + n.slice(1), t.st.callbacks[n] && t.st.callbacks[n].apply(t, e.isArray(i) ? i : [i]))
}, E = function(n) {
return n === s && t.currTemplate.closeBtn || (t.currTemplate.closeBtn = e(t.st.closeMarkup.replace("%title%", t.st.tClose)), s = n), t.currTemplate.closeBtn
}, _ = function() {
e.magnificPopup.instance || (t = new w, t.init(), e.magnificPopup.instance = t)
}, S = function() {
var e = document.createElement("p").style,
t = ["ms", "O", "Moz", "Webkit"];
if (void 0 !== e.transition) return !0;
for (; t.length;)
if (t.pop() + "Transition" in e) return !0;
return !1
};
w.prototype = {
constructor: w,
init: function() {
var n = navigator.appVersion;
t.isIE7 = -1 !== n.indexOf("MSIE 7."), t.isIE8 = -1 !== n.indexOf("MSIE 8."), t.isLowIE = t.isIE7 || t.isIE8, t.isAndroid = /android/gi.test(n), t.isIOS = /iphone|ipad|ipod/gi.test(n), t.supportsTransition = S(), t.probablyMobile = t.isAndroid || t.isIOS || /(Opera Mini)|Kindle|webOS|BlackBerry|(Opera Mobi)|(Windows Phone)|IEMobile/i.test(navigator.userAgent), o = e(document), t.popupsCache = {}
},
open: function(n) {
i || (i = e(document.body));
var r;
if (n.isObj === !1) {
t.items = n.items.toArray(), t.index = 0;
var s, l = n.items;
for (r = 0; l.length > r; r++)
if (s = l[r], s.parsed && (s = s.el[0]), s === n.el[0]) {
t.index = r;
break
}
} else t.items = e.isArray(n.items) ? n.items : [n.items], t.index = n.index || 0; if (t.isOpen) return t.updateItemHTML(), void 0;
t.types = [], a = "", t.ev = n.mainEl && n.mainEl.length ? n.mainEl.eq(0) : o, n.key ? (t.popupsCache[n.key] || (t.popupsCache[n.key] = {}), t.currTemplate = t.popupsCache[n.key]) : t.currTemplate = {}, t.st = e.extend(!0, {}, e.magnificPopup.defaults, n), t.fixedContentPos = "auto" === t.st.fixedContentPos ? !t.probablyMobile : t.st.fixedContentPos, t.st.modal && (t.st.closeOnContentClick = !1, t.st.closeOnBgClick = !1, t.st.showCloseBtn = !1, t.st.enableEscapeKey = !1), t.bgOverlay || (t.bgOverlay = k("bg").on("click" + h, function() {
t.close()
}), t.wrap = k("wrap").attr("tabindex", -1).on("click" + h, function(e) {
t._checkIfClose(e.target) && t.close()
}), t.container = k("container", t.wrap)), t.contentContainer = k("content"), t.st.preloader && (t.preloader = k("preloader", t.container, t.st.tLoading));
var c = e.magnificPopup.modules;
for (r = 0; c.length > r; r++) {
var d = c[r];
d = d.charAt(0).toUpperCase() + d.slice(1), t["init" + d].call(t)
}
T("BeforeOpen"), t.st.showCloseBtn && (t.st.closeBtnInside ? (x(p, function(e, t, n, i) {
n.close_replaceWith = E(i.type)
}), a += " mfp-close-btn-in") : t.wrap.append(E())), t.st.alignTop && (a += " mfp-align-top"), t.fixedContentPos ? t.wrap.css({
overflow: t.st.overflowY,
overflowX: "hidden",
overflowY: t.st.overflowY
}) : t.wrap.css({
top: I.scrollTop(),
position: "absolute"
}), (t.st.fixedBgPos === !1 || "auto" === t.st.fixedBgPos && !t.fixedContentPos) && t.bgOverlay.css({
height: o.height(),
position: "absolute"
}), t.st.enableEscapeKey && o.on("keyup" + h, function(e) {
27 === e.keyCode && t.close()
}), I.on("resize" + h, function() {
t.updateSize()
}), t.st.closeOnContentClick || (a += " mfp-auto-cursor"), a && t.wrap.addClass(a);
var u = t.wH = I.height(),
m = {};
if (t.fixedContentPos && t._hasScrollBar(u)) {
var g = t._getScrollbarSize();
g && (m.marginRight = g)
}
t.fixedContentPos && (t.isIE7 ? e("body, html").css("overflow", "hidden") : m.overflow = "hidden");
var C = t.st.mainClass;
return t.isIE7 && (C += " mfp-ie7"), C && t._addClassToMFP(C), t.updateItemHTML(), T("BuildControls"), e("html").css(m), t.bgOverlay.add(t.wrap).prependTo(t.st.prependTo || i), t._lastFocusedEl = document.activeElement, setTimeout(function() {
t.content ? (t._addClassToMFP(v), t._setFocus()) : t.bgOverlay.addClass(v), o.on("focusin" + h, t._onFocusIn)
}, 16), t.isOpen = !0, t.updateSize(u), T(f), n
},
close: function() {
t.isOpen && (T(c), t.isOpen = !1, t.st.removalDelay && !t.isLowIE && t.supportsTransition ? (t._addClassToMFP(C), setTimeout(function() {
t._close()
}, t.st.removalDelay)) : t._close())
},
_close: function() {
T(l);
var n = C + " " + v + " ";
if (t.bgOverlay.detach(), t.wrap.detach(), t.container.empty(), t.st.mainClass && (n += t.st.mainClass + " "), t._removeClassFromMFP(n), t.fixedContentPos) {
var i = {
marginRight: ""
};
t.isIE7 ? e("body, html").css("overflow", "") : i.overflow = "", e("html").css(i)
}
o.off("keyup" + h + " focusin" + h), t.ev.off(h), t.wrap.attr("class", "mfp-wrap").removeAttr("style"), t.bgOverlay.attr("class", "mfp-bg"), t.container.attr("class", "mfp-container"), !t.st.showCloseBtn || t.st.closeBtnInside && t.currTemplate[t.currItem.type] !== !0 || t.currTemplate.closeBtn && t.currTemplate.closeBtn.detach(), t._lastFocusedEl && e(t._lastFocusedEl).focus(), t.currItem = null, t.content = null, t.currTemplate = null, t.prevHeight = 0, T(d)
},
updateSize: function(e) {
if (t.isIOS) {
var n = document.documentElement.clientWidth / window.innerWidth,
i = window.innerHeight * n;
t.wrap.css("height", i), t.wH = i
} else t.wH = e || I.height();
t.fixedContentPos || t.wrap.css("height", t.wH), T("Resize")
},
updateItemHTML: function() {
var n = t.items[t.index];
t.contentContainer.detach(), t.content && t.content.detach(), n.parsed || (n = t.parseEl(t.index));
var i = n.type;
if (T("BeforeChange", [t.currItem ? t.currItem.type : "", i]), t.currItem = n, !t.currTemplate[i]) {
var o = t.st[i] ? t.st[i].markup : !1;
T("FirstMarkupParse", o), t.currTemplate[i] = o ? e(o) : !0
}
r && r !== n.type && t.container.removeClass("mfp-" + r + "-holder");
var a = t["get" + i.charAt(0).toUpperCase() + i.slice(1)](n, t.currTemplate[i]);
t.appendContent(a, i), n.preloaded = !0, T(m, n), r = n.type, t.container.prepend(t.contentContainer), T("AfterChange")

Character limit!!!!!

最佳答案

这是一种解决方案。您可以执行以下步骤来重新初始化插件每次ajax被触发

  1. 克隆现有的弹出链接(没有事件)
  2. 删除原有元素
  3. 附加克隆项目
  4. 再次初始化插件。 (因此插件将为所有新添加的元素添加事件)。

一旦 ajax 成功并创建新元素,请尝试运行以下代码。

var $popups = jQuery('.property-video-popup');
var $clones = $popups.clone();

$clones.each(function(index,elem){
var $popup = $popups.eq(index);
jQuery(elem).insertAfter($popup);
$popup.remove();
})

jQuery('.property-video-popup').magnificPopup({
type: 'iframe'
});

关于jquery - 使用 AJAX 加载 jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32195554/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com