Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
279 views
in Technique[技术] by (71.8m points)

javascript - How to style the string of an <alt> tag?

Is there a way to style the alt? With javascript? With pseudo attributes? like this:

<img src="foo.jpg" alt="<h1>This is the caption</h1><p>This is some text</p><p>Another text</p>" />

??

And javascript (I use JQuery framework) does something like this?

Get value of <alt> from <img>
make value between <h1></h1> css font-size:22px; color:green;
make value between <p></p> css font-size:14px; color:black;

BTW: The javascript reads the alt-tag of the image and shows them on the screen.. so this alt text will not only shown If the img isnt loaded.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's the alt attribute, not tag, and it's not possible to style it directly. It's simply a piece of text that is displayed if the image fails to load. You can style the <img> element though and it will affect the styling of the alternative text in most browsers (though not IE).

If you were referring to the tooltip that appears in some browsers when you hover over an image, that's achieved with the title attribute and works on all elements, not just images. The alt attribute is also used by IE for this purpose on images, but this is not standard. Either way, title tooltips cannot be styled. If you need fancier tooltips, you'll have to create your own. I imagine there's a plethora of such things out there.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...