简体   繁体   English

有没有一种方法可以使用fancybox在'alt'属性中添加HTML代码?

[英]Is there a way to add HTML code inside the 'alt' attribute using fancybox?

I am trying to see if it is possible to add html code inside the 'alt' tag of an image either through the html or using some kind of javascript. 我正在尝试查看是否有可能通过html或使用某种javascript在图像的“ alt”标记内添加html代码。 I am using fancybox as my image gallery. 我正在使用fancybox作为我的图片库。 I'm trying to use fancybox as a form to display an image and on the description of the image, add information with style (like bullet points and breaks) and also add a button that will take you to a different page. 我正在尝试使用fancybox作为一种形式来显示图像,并在图像的描述上添加带有样式的信息(例如,项目符号和中断),并添加一个将您带到其他页面的按钮。 Currently I have the button working but that button is in the javascript so every fancybox image has that button and the same url. 目前,我可以使用该按钮,但是该按钮在javascript中,因此每个fancybox图像都具有该按钮和相同的url。 And i want to have different links to each button on each image. 我想对每个图像上的每个按钮有不同的链接。

Here is the javascript that i have that currently displays the alt text under the image in the fancybox. 这是我目前在fancybox中的图像下方显示alt文本的javascript。

$(".fancybox").fancybox({               
                padding : 0,
                beforeShow: function () {
                    this.title = $(this.element).attr('title');
                    this.title = '<h4>' + this.title + '</h4>' + '<div style="width:100%; height: 150px; overflow: auto;">' + $(this.element).parent().find('img').attr('alt') + '<a class="button button-small" href="http://www.google.com"> Sign Up </a>' + '</div>';
                },
                helpers : {
                    title : { type: 'inside' },
                }
            });

The html in the index.html for that fancybox is: 该fancybox的index.html中的html是:

<li class="item-thumbs span3 design">
                                <!-- Fancybox - Gallery Enabled - Title - Full Image -->
                                <a class="hover-wrap fancybox" data-fancybox-group="gallery" title="The City" href="_include/img/work/full/image-01-full.jpg">
                                    <span class="overlay-img"></span>
                                    <span class="overlay-img-thumb font-icon-plus"></span>
                                </a>
                                <!-- Thumb Image and Description -->
                                <img src="_include/img/work/thumbs/image-01.jpg" alt="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus quis elementum odio. Curabitur pellentesque, dolor vel pharetra mollis.">
                            </li>

The alt attribute (not tag) is by definition plain text, so whatever you insert there will be taken as plain text (not markup) by browsers. alt属性(不是标签)按照定义是纯文本,因此,您在其中插入的任何内容都将被浏览器视为纯文本(而非标记)。 If you mess around with code that inserts strings that look like HTML tags into an alt attribute value and then parse and process them as markup, this is no different with similar play with other attributes. 如果您弄乱了将看起来像HTML标记的字符串插入alt属性值,然后将其解析并处理为标记的代码,那么这与使用其他属性进行类似的操作没有什么不同。

The alt attribute has a well-defined job, to act as a textual replacement for the image when the image is not displayed but eg spoken, rendered with a Braille device, or displayed as text. alt属性具有定义明确的工作,当图像未显示但例如说话,用盲文设备渲染或显示为文本时,可以用作图像的文本替换。 It is thus unproductive to try to use it for other purposes. 因此,尝试将其用于其他目的是徒劳的。

Similar considerations apply to the title attribute. 类似的注意事项也适用于title属性。

To use data that will be parsed as HTML, it is thus much better to use a custom attribute, specifically a data-* attribute , like data-desc , or whatever you prefer. 要使用将被解析为HTML的数据,最好使用自定义属性,特别是data-*属性 (例如data-desc )或您喜欢的任何属性 They have no default processing in browsers or search engines, so they are safe to use for private purposes. 它们在浏览器或搜索引擎中没有默认处理,因此可以安全地用于私人目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM