简体   繁体   English

图片标题而不是alt

[英]image title instead of alt

I have a wordpress page with the event manager plugin. 我有一个带有事件管理器插件的wordpress页面。 Last year we launched the bookings option form for our climbing camps and for our courses. 去年,我们为登山营和课程推出了预订选项表。 Now we think the camps would be more interresting if the members could see the list of the attendees. 现在我们认为,如果成员可以看到参加者的名单,那么营地将更加有趣。 It can be setting with the #_ATTENDEES which is showing the useravatar of the attendees. 可以使用显示与会者的用户头像的#_ATTENDEES进行设置。 I'd like to display the names of the attendees with the good old method, with the mouse over trick. 我想用一种很好的旧方法显示与会者的姓名,并将鼠标悬停在上面。 I see in the source code of my event page, in the code of displayed avatar, there is an 'alt' tag which contains the right member name. 我在活动页面的源代码中,在显示的头像的代码中看到一个“ alt”标记,其中包含正确的成员名称。 But it will not displaying if I move my mouse over the picture. 但是,如果我将鼠标移到图片上,它将不会显示。 It is not matter if I'm using chrome or other browsers. 我使用的是chrome还是其他浏览器都没关系。 The solution would be: I think the 'alt' tag in the 'img src' row generated automatically, because I didn't find that in the attendees.php. 解决的办法是:我认为'img src'行中的'alt'标签是自动生成的,因为我在与会者网站php中找不到它。 If it would be possible to change the 'alt' tag to 'title' then it will be display. 如果可以将“ alt”标签更改为“ title”,则将显示该标签。 How can I solve this? 我该如何解决?

In the support of EM, I got an answer to this site, with this link: Use 'alt' value to dynamically set 'title' But I'm not an expert of jquery solutions, but as I read it, there is a solution to take the attributes of the image-alt tag into the attributes of the a-title tag. 在EM的支持下,我通过以下链接获得了该站点的答案: 使用'alt'值动态设置'title'但是我不是jquery解决方案的专家,但是在我阅读本文时,有一个解决方案将image-alt标签的属性带入a-title标签的属性。 But I don't have title tag! 但是我没有标题标签! My code is following: 我的代码如下:

 <img src="http://webaddress/wp-content/uploads/2016/03/useravatar-80x80.jpg" width="50" height="50" alt="Long John" class="avatar avatar-50 wp-user-avatar wp-user-avatar-50 alignnone photo"> 

I need to change alt=”Long John” to title=”Long John”. 我需要将alt =“ Long John”更改为title =“ Long John”。 How can I do this? 我怎样才能做到这一点?

You can find all img elements that have an alt and don't have a title by using jQuery's $() function with the CSS selector img[alt]:not([title]) . 通过将jQuery的$()函数与CSS选择器img[alt]:not([title])使用,可以找到所有具有alt且没有titleimg元素。 Then use jQuery's attr function to assign a title attribute to them from alt . 然后使用jQuery的attr函数从alt为其分配一个title属性。 You probably want to use jQuery's "ready" feature to do it when the page is loaded, since Wordpress makes putting scripts in the right place problematic I'm told: 您可能想在页面加载时使用jQuery的“就绪”功能来执行此操作,因为Wordpress使得将脚本放置在正确的位置存在问题,我被告知:

$(function() {
    $("img[alt]:not([title])").attr("title", function() {
        return this.alt;
    });
});

If you're not already using jQuery, you don't need to add it for this. 如果您尚未使用jQuery,则无需为此添加它。 Here's a near equivalent to the above without it that works in all vaguely-modern browsers: 这几乎等同于上述内容,但没有它可以在所有模糊的现代浏览器中使用:

document.addEventListener("DOMContentLoaded", function() {
    Array.prototype.forEach.call(document.querySelector("img[alt]:not([title])", function(img) {
        img.title = img.alt;
    });
}, false);

More: 更多:

If you need to support obsolete browsers without addEventListener , this answer has a cross-browser option for you. 如果您需要不带addEventListener过时浏览器, 此答案为您提供了一个跨浏览器选项。 Said browser will also need a polyfill for forEach , which is described in the linked page above. 所述浏览器还将需要forEach ,在上面的链接页面中对此进行了描述。

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

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