简体   繁体   English

为什么是 <img> alt属性不一定总是正确显示

[英]why is the <img> alt attribute not always properly displayed

So I have this php script that output an html table with data about some files (like filename, filesize, etc...) 因此,我有这个php脚本,可输出带有一些文件(例如文件名,文件大小等)数据的html表。

I have this javascript function that displays the picture when you hover a tag belonging to the class "preview". 我有这个javascript函数,当您将鼠标悬停在属于“ preview”类的标签时,该图片将显示图片。 For example, if a filename is: somePic.jpg, when you hover somePic.jpg in the table, the picture will appear next to your mouse. 例如,如果文件名是:somePic.jpg,则将鼠标悬停在表中时,图片将出现在鼠标旁边。

Now not all these files are pictures, some are .mp3, so of course when you hover them, the javascript cannot display the picture. 现在,并非所有这些文件都是图片,有些不是.mp3,因此,当您将它们悬停时,javascript无法显示图片。 To deal with this case, I added in the tag (generated by the javascript function), an alt attribute: alt='Preview not available for this type of content.' 为了处理这种情况,我在标记(由javascript函数生成)中添加了一个alt属性:alt ='预览不适用于此类内容。'

And here is my problem, sometimes it works, but sometimes it doesn't! 这是我的问题,有时可以,但是有时不行! Sometimes you start hovering .mp3 links, and the alt attribute is displayed, then you hover a picture, the picture is displayed, then you hover an .mp3 again, and the alt isn't displayed anymore, but the "broken image" image (the little red cross) is displayed instead... 有时您开始悬停.mp3链接,并显示alt属性,然后悬停图片,显示该图片,然后再次悬停.mp3,并且不再显示alt,但显示“残破的图像”图像而是显示(红色小十字)...

Sure I could parse the filenames and detect when it's an mp3 and then deal with the case, but I thought the alt attribute was suppose to achieve this... but it's buggy... 当然,我可以解析文件名并检测它是否为mp3,然后再处理大小写,但是我认为alt属性应该可以实现此目的...但是它有很多问题...

Any idea? 任何想法? Does anyone already faced this problem? 有没有人遇到过这个问题?

It seems as if you are confusing the alt attribute and the title attribute. 好像您混淆了alt属性和title属性。

The alt attribute is an img -specific attribute to assign alternative content to be rendered in the case that the image cannot be. alt属性是img特定的属性,用于分配在图像无法显示的情况下要呈现的替代内容。 title is a general attribute used to provide a tooltip when hovering over the element. title是一个通用属性,用于将鼠标悬停在元素上时提供工具提示。

Some broken UAs cough IE cough treat alt as title on images; 一些破碎的UA 咳嗽 IE 咳嗽alt用作图像的title the root of much confusion. 造成混乱的根源。

In general alt is for when images are disabled, rather than as a backstop for error conditions. 通常, alt用于禁用图像,而不是作为错误情况的支持。 Whilst IMO it's a desirable feature that the alt text be displayed in error conditions, the standard doesn't say it has to: 尽管IMO的一个理想功能是在错误情况下显示alt文本,但标准并没有说必须:

For user agents that cannot display images, forms, or applets, this attribute specifies alternate text. 对于无法显示图像,表单或小程序的用户代理,此属性指定备用文本。

and indeed browser behaviour varies. 实际上,浏览器的行为各不相同。 WebKit (Safari, Chrome) will only display a broken image icon and never the alt text; WebKit(Safari,Chrome)将仅显示损坏的图像图标,而不会显示替换文本; IE tries to render the alt text inside a box with an image icon (which frequently displays poorly). IE会尝试在带有图像图标(通常显示效果不佳)的框中显示替代文本。

So you shouldn't rely on alt text being displayed on error. 因此,您不应该依赖在错误时显示替代文本。

Sure I could parse the filenames and detect when it's an mp3 当然,我可以解析文件名并检测它何时是mp3

Yes, I think you'll have to. 是的,我认为您必须这样做。 Not that that's possible either in the general case since filename/URL extension may well have nothing to do with the actual file type. 在一般情况下,这都是不可能的,因为文件名/ URL扩展名可能与实际文件类型无关。 However if you control all the URLs yourself you may be able to guarantee that all MP3s are called ...mp3 . 但是,如果您自己控制所有URL,则可以保证所有MP3都称为...mp3

How about letting the PHP detect the filetype and let it render the javascript inline only on images. 如何让PHP检测文件类型并使其仅在图像上内嵌javascript。

You should also check out FireBug and see what is happening when you hover over the images. 您还应该签出FireBug,并将鼠标悬停在图像上,看看会发生什么情况。

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

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