我是 Foundation 的新手,我正在尝试创建一个 jQuery 插件来动态地用图像填充模式,然后在单击某个按钮后打开模式。但是模态不起作用。任何帮助将不胜感激。
<!DOCTYPE html>
<html lang="en">
<head>
<title>Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/foundation/6.3.0/css/foundation.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/foundation/6.3.0/js/foundation.js"></script>
<style>
img {
max-width: 100px;
width: 100%;
height: auto;
padding: 1em;
}
</style>
</head>
<body>
<a id="Slider" class="button">Open Modal</a>
<script id="slideShow" type="text/template">
<div class="reveal-modal" data-reveal aria-labelledby="modalTitle" aria-hidden="true" role="dialog">
<div class="modal-body">
</div>
<a class="close-reveal-modal" aria-label="Close">×</a>
</div>
</script>
<script>
(function($) {
/*
* expecting:
* - array of attributes for img tag
* - text/template tag
*/
$.fn.imageSlider = function(array, element, location) {
// check if element is instance of jQuery object, if not then make it
if (element instanceof jQuery == false) {
element = $(element);
}
// base template of image
var template = "<img />";
// loop through all images and add them to element
for (var obj of array) {
element.find(".modal-body").append($(template).attr(obj));
}
// random ID generator
var rand = Math.random().toString(36).substring(7);
// show modal on click over image
element.attr("id", rand);
this.attr("data-reveal-id", rand);
// append element to this
location.append(element);
}
})(jQuery);
var array = [];
for (var i = 1; i < 10; i++) {
array.push({
src: "https://placehold.it/350x350"
});
}
$(function() {
$("#Slider").imageSlider(array, $("#slideShow").html(), $("body"));
$(document).foundation();
});
</script>
</body>
</html>
最佳答案
您可以使用 AJAX 来做到这一点 - 我们写了一些关于如何以两种不同方式做到这一点的类(class): http://zurb.com/university/lessons/ajaxing-dynamic-content-with-foundation 和 http://zurb.com/university/lessons/dynamically-update-your-web-pages
关于javascript - 尝试创建一个 jQuery 插件来动态填充 Foundation 模态中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41477596/