Fancybox-是否可以使用javascript变量字符串作为HTML内容
Fancybox - is it possible to use a javascript variable string as the HTML content?
我知道Fancybox的说明表明,内容可以通过四种方式中的任何一种传递;图像、内联、iFrame或Ajax。
然而,我想知道我是否有这样的字符串:
var html = "<p>Some html content</p>";
如果我可以使用这些内容调用一个fancybox窗口?
EDIT关于JFK下面的回答,根据条件提供内容的最佳方式是什么?例如,假设我有两个不同的fancybox链接:
<a href="#" class="fancybox class-one">Click me</a>
<a href="#" class="fancybox class-two">Click me</a>
所以,当点击任一链接时,我想根据第二个类名显示不同的内容?
是的,这是可能的。您也可以将html
设置为content
的type
,这样就可以工作了
var html = "<p>Some html content</p>";
jQuery(document).ready(function ($) {
$(".fancybox").fancybox({
type: "html", // not an image but html
content: html // overrides fancybox content
});
}); // ready
请注意API选项content
会覆盖扇盒content
,而不管绑定到扇盒的锚的href
属性中设置了什么,因此此
<a class="fancybox" href="images/01.jpg">Open Fancybox</a>
仍将在fancybox中显示javascript变量的值,而不是在href
中显示图像
请参阅JSFIDDLE
编辑:
根据中选择器的类提供内容
<a href="#" class="fancybox class-one">Click me</a>
<a href="#" class="fancybox class-two">Click me</a>
我认为最好使用switch
语句(在.on()
方法中)来动态设置html
变量的值。然后以编程方式启动fancybox,如:
var html; // initialize variable globally
jQuery(document).ready(function ($) {
$(".fancybox").on("click", function () {
switch (this.className) {
case "fancybox class-one":
html = "<p>Some html content for class-one</p>";
break;
case "fancybox class-two":
html = "<p>And this the html content for class-two</p>";
break;
default:
html = "<p>html default content</p>";
};
$.fancybox({
type: "html",
content: html
});
return false;
});
}); // ready
请参阅分叉JSFIDDLE
相关文章:
- 如何根据html内容使用ng类-AngularJS
- JavaScript在其他页面上用html内容填充文本框
- Rails:如何在浏览器中显示动态html内容,而不会对其进行转义,也不会引起XSS攻击
- 使用javascript动态创建html内容/元素
- 无法将大型html内容POST到Chrome上的服务器
- 使用li元素的html内容更改该元素的背景
- 如何从ajax返回两个html内容
- jQuery获取子DIV的HTML内容
- HTML内容下推到固定背景图像下方
- 在angular中,使用ng repeat生成动态html内容
- 使用$watch更改html内容
- 如何使用window.showModalDialog()显示HTML内容
- 如何在使用 UIWebView 编辑 HTML 内容时正确调整输入语言更改 (LTR/RTL) 的文本对齐方式
- 获取垂直溢出容器的 html 内容
- 为数组中的每个项目追加 html 内容
- Dojo:在 iframe 加载时解析 HTML 内容
- 允许 Itemp 在 Enyo 中使用 HTML 内容
- 从选项卡中获取 HTML 内容
- 可以将HTML内容放在画布上(登录页)
- 只有当类是硬编码的时,才可以使用jQuery来显示/隐藏html内容