简体   繁体   English

将图像插入标题 HTML 属性

[英]Insert image into the title HTML attribute

I use poshy jQuery bubble and I need to insert an image inside the bubble.我使用豪华的 jQuery 气泡,我需要在气泡内插入图像。

With this plugin, the text inside the bubble was generated with the title attribute (I have different text, its not the same image for each link, it's why I need that).使用这个插件,气泡内的文本是使用标题属性生成的(我有不同的文本,每个链接的图像都不一样,这就是我需要它的原因)。

<a class="demo-tip-yellow" title="<img src='/static/images/school/parents/btn_nav  /btn_change.png'> " href="<?=site_url('school/parents/children/'.$item['id'])?>">

Is it possible to insert a string inside the title attribute, if yes, how?是否可以在 title 属性中插入一个字符串,如果可以,如何?

Please don't misuse the title attribute , it is a very important one also in terms of accessibility (and even SEO).请不要滥用title属性,它在可访问性(甚至 SEO)方面也是一个非常重要的属性。 It should only contain simple text.它应该只包含简单的文本。

You could use the data- attributes to store the image source and retrieve it through jQuery.您可以使用data-属性来存储图像源并通过 jQuery 检索它。 This is the best way you could achieve this.这是实现这一目标的最佳方式。

Using this HTML:使用这个 HTML:

<a data-tooltipimage="/static/images/school/parents/btn_nav/btn_change.png" 
    title="Something descriptive" 
    href="#" 
    class="demo-tip-yellow">

you can easily retrieve the attribute through您可以通过以下方式轻松检索属性

$('demo-tip-yellow').attr('data-tooltipimage');

or from jQuery 1.4.3, you can simply use或从 jQuery 1.4.3 开始,您可以简单地使用

$('demo-tip-yellow').data('tooltipimage');

because jQuery automatically fetches data- attributes into the data object.因为 jQuery 会自动将data-属性提取到数据 object 中。

I don't know this exact plugin, but if you check the documentation, you certainly have a way to control where does the plugin get its data from and how it is displayed.我不知道这个确切的插件,但如果你查看文档,你肯定有办法控制插件从哪里获取数据以及如何显示。

I checked Poshy Tip documentation: http://vadikom.com/demos/poshytip/我检查了 Poshy Tip 文档: http://vadikom.com/demos/poshytip/

There is a way to set the content by a function, so the following may very well work:有一种方法可以通过 function 设置内容,因此以下方法可能会很好地工作:

<a href="http://linkurl" id="link" data-tooltipimage="http://imageurl" title="sometitle">link...</a>
<script type="text/javascript">
$('#link').each(function() {
    var img_url = $(this).data("tooltipimage");
    var tooltip = img_url ? "<img src='"+img_url+"' alt='' />" : $(this).attr("title");
    $(this).attr("title","");
    $(this).poshytip({
        content: function(updateCallback) {
            return tooltip;
        }
    });
});
</script>

You should set the data-tooltipimage only when you want image tooltip.只有在需要图像工具提示时才应设置data-tooltipimage工具提示图像。

EDIT: an other approach, with a hidden element, where you can put anything:编辑:另一种方法,带有隐藏元素,您可以在其中放置任何内容:

<a href="http://linkurl" id="link" title="sometitle">
    link...
    <span class="hidden-tooltip-data" style="display: none;"><img />...</span>
</a>
<script type="text/javascript">
$('#link').each(function() {
    var tooltip = $(".hidden-tooltip-data",this).html();
    $(this).attr("title","");
    $(this).poshytip({
        content: function(updateCallback) {
            return tooltip;
        }
    });
});
</script>

EDIT 2: and another:编辑2:另一个:

<a id="link1" href="http://linkurl" title="sometitle">link...</a>
<span id="link1_tooltip" style="display: none;"><img /><a></a>...</span>
<script type="text/javascript">
$('#link').each(function() {
    var linkid = $(this).attr("id");
    var tooltip = $("#"+linkid+"_tooltip").html();
    $(this).attr("title","");
    $(this).poshytip({
        content: function(updateCallback) {
            return tooltip;
        }
    });
});
</script>

I would go with this HTML:我会用这个 HTML go:

<a class="tipped" href="/path/to/something.html">
    <img src="something-tooltip.png" alt="Real text of the tooltip."/>
    Something
</a>

Combined with this CSS:结合这个 CSS:

a.tipped img {
    display: none;
}

a.tipped:hover img {
    position: absolute;
    display: block;
    margin: 10px 0 0 10px;
}

You won't need JS, but of course you can use it to animate the tooltip.您不需要 JS,但您当然可以使用它来为工具提示设置动画。 In this case you can drop the a.tipped:hover img rule.在这种情况下,您可以删除a.tipped:hover img规则。

This way you won't commit an assault against the web,这样您就不会攻击 web,

Which plugin are you using?你用的是哪个插件?

Try to html encode <img src='/static/images/school/parents/btn_nav/btn_change.png'> .尝试 html 编码<img src='/static/images/school/parents/btn_nav/btn_change.png'> In PHP it's htmlentities() I think.在 PHP 中,我认为是htmlentities()

while you should not be storing markup in the title tag, you could use:虽然您不应该在标题标签中存储标记,但您可以使用:

<a class="demo-tip-yellow" title="<?php echo htmlentities(<img src='/static/images/school/parents/btn_nav  /btn_change.png'>);?> " href="<?=site_url('school/parents/children/'.$item['id'])?>">

Having the > inside an attribute might break the HTML as well.在属性中使用 > 也可能会破坏 HTML。 If you just want to have an image inside an overlay/bubble/lightbox i suggest looking at http://fancybox.net/如果您只想在叠加/气泡/灯箱内放置图像,我建议您查看http://fancybox.net/

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

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