简体   繁体   English

单击图像时显示ALT文本

[英]Show ALT-Text when clicking on image

How to show the alt-text of an image when clicking on the image? 单击图像时如何显示图像的替代文本? I think I need to use jQuery. 我想我需要使用jQuery。

This is the example of the HTML-Code: 这是HTML代码的示例:

<div id="images">
     <img src="/img/image.png" alt="Text-1">
     <img src="/img/image.png" alt="Text-2">
     <img src="/img/image.png" alt="Text-3">
     <img src="/img/image.png" alt="Text-4">
</div>

And the text should be shown in a separate div : 文本应显示在单独的div

<div id="show-text"></div>

But only one alt-text should be shown at once. 但是一次只能显示一个替代文本。 So by clicking on the first Image "Text-1" should be shown, by clicking on the second image the "Text-1" should be replaced by "Text-2". 因此,通过单击第一个图像应显示“ Text-1”,通过单击第二个图像,应将“ Text-1”替换为“ Text-2”。

I think this is what you're looking for. 我认为这就是您要寻找的。

  $("#images img").on("click", function() { $("#show-text").text($(this).attr("alt")); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="images"> <img src="/img/image.png" alt="Text-1"> <img src="/img/image.png" alt="Text-2"> <img src="/img/image.png" alt="Text-3"> <img src="/img/image.png" alt="Text-4"> </div> <div id="show-text"></div> 

CSS Solution? CSS解决方案?

img:focus::before {
    content: attr(alt);
}

You'd need to set the placement and so on, and it's not strictly an answer to the original question - but it's an alternative. 您需要设置放置位置,依此类推,严格来说,这并不是对原始问题的答案-但这是另一种选择。

Here, when you click on an image. 在这里,当您单击图像时。 This jQuery script will get the img clicked and get its attribute and then show it in your div. 此jQuery脚本将单击img,并获取其属性,然后将其显示在div中。

 $('#images img').on('click', function(){ $('#show-text').html($(this).attr('alt')); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="images"> <img src="/img/image.png" alt="Text-1"> <img src="/img/image.png" alt="Text-2"> <img src="/img/image.png" alt="Text-3"> <img src="/img/image.png" alt="Text-4"> </div> <div id="show-text"></div> 

Here is solution: 这是解决方案:

You need to attach a click event handler for every image and get it's alt text, using alt attribute. 您需要为每个图像attach一个click事件处理程序,并使用alt属性获取其alt文本。 When you have alt name, you should display in show-text div, using .text method. 当您具有alt名称时,应使用.text方法在show-text div中show-text

For getting attribute you have to use .attr method. 为了获得属性,您必须使用.attr方法。

 $('#images img').click(function(){ $('#show-text').text($(this).attr('alt')); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="images"> <img src="/img/image.png" alt="Text-1"> <img src="/img/image.png" alt="Text-2"> <img src="/img/image.png" alt="Text-3"> <img src="/img/image.png" alt="Text-4"> </div> <div id="show-text"></div> 

For future viewers who may not be using jQuery this is a reasonably straightforward example which does not use any libraries or frameworks. 对于可能不使用jQuery的未来查看者,这是一个相当简单的示例,其中不使用任何库或框架。 I've used some syntactic sugar language features that were introduced in the ECMAScript 2015 Language Specification for simplicity, but this could be easily achieved without using those features. 为了简单起见,我使用了ECMAScript 2015语言规范中引入的一些语法糖语言功能,但是无需使用这些功能就可以轻松实现。

Bound

This example describes a method of directly binding the event listener to each image. 本示例描述了将事件侦听器直接绑定到每个图像的方法。 This should be used if images are not added or removed dynamically, or if you need to exclude some images. 如果没有动态添加或删除图像,或者需要排除某些图像,则应使用此选项。

  1. Select the target element 选择目标元素
  2. Select the images and convert the NodeList to an array 选择图像并将NodeList转换为数组
  3. Define a listener function which assigns the content of the alt attribute to the textContent property of the target Node object. 定义一个侦听器函数,该函数将alt属性的内容分配给目标Node对象的textContent属性。
  4. Iterate the images, assigning the event listener to each image. 迭代图像,将事件侦听器分配给每个图像。
  5. Profit! 利润!
const target = document.getElementById('show-text');
const images = [...document.querySelectorAll('#images img')];
const listener = event => target.textContent = event.target.alt;
images.forEach(element => element.addEventListener('click', listener, false));

 const target = document.getElementById('show-text'); const images = [...document.querySelectorAll('#images img')]; const listener = event => target.textContent = event.target.alt; images.forEach(element => element.addEventListener('click', listener, false)); 
 img { cursor: pointer } 
 <div id="images"> <img src="http://placehold.it/100x100?text=1" alt="Text-1"> <img src="http://placehold.it/100x100?text=2" alt="Text-2"> <img src="http://placehold.it/100x100?text=3" alt="Text-3"> <img src="http://placehold.it/100x100?text=4" alt="Text-4"> </div> And the text should be shown in a separate div: <div id="show-text"></div> 


Delegated 委托

This example describes a method of listening to events on a common parent element. 本示例描述了侦听公共父元素上的事件的方法。 This method should be used if images will be dynamically added and removed. 如果将动态添加和删除图像,则应使用此方法。

  1. Select the target element 选择目标元素
  2. Select the common parent element 选择公共父元素
  3. Define a listener function which checks to see if the clicked element is an image, then—if it is—assigns the content of the alt attribute to the textContent property of the target Node object. 定义一个侦听器函数,该函数检查以查看clicked元素是否为图像,然后将alt属性的内容分配给目标Node对象的textContent属性。
  4. Assign the listener function to the common parent element 将侦听器功能分配给公共父元素
  5. Profit! 利润!
const target = document.getElementById('show-text');
const images = document.getElementById('images');
const listener = event =>
  event.target.tagName === 'IMG' && (target.textContent = event.target.alt);
images.addEventListener('click', listener, true);

 const target = document.getElementById('show-text'); const images = document.getElementById('images'); const listener = event => event.target.tagName === 'IMG' && (target.textContent = event.target.alt); images.addEventListener('click', listener, true); 
 img { cursor: pointer } 
 <div id="images"> <img src="http://placehold.it/100x100?text=1" alt="Text-1"> <img src="http://placehold.it/100x100?text=2" alt="Text-2"> <img src="http://placehold.it/100x100?text=3" alt="Text-3"> <img src="http://placehold.it/100x100?text=4" alt="Text-4"> </div> And the text should be shown in a separate div: <div id="show-text"></div> 


Cache Your jQuery Objects 缓存您的jQuery对象

If you do have to use jQuery, I cannot emphasize enough the importance of caching your jQuery objects. 如果您确实必须使用jQuery,则我不能足够强调缓存jQuery对象的重要性。

When you have something like this: 当你有这样的事情:

$('#images img').on('click', function(){
  $('#show-text').html($(this).attr('alt'));
});

You are querying the DOM for an element matching the #show-text selector every time an image is clicked. 每次单击图像时,您正在DOM中查询与#show-text选择器匹配的元素。 This may not seem important when you have only a few items, but when you get into situations where the DOM is large, this can take more and more time to complete. 当您只有几个项目时,这似乎并不重要,但是当您遇到DOM很大的情况时,这可能会花费越来越多的时间。

To save a lot of overhead, simply cache the jQuery objects like so: 为了节省大量开销,只需缓存jQuery对象,如下所示:

const target = $('#show-text');
const images = $('#images img');
images.on('click', event => target.text(event.target.alt));

(Yeah, I know I changed some other things. I couldn't help myself) (是的,我知道我改变了一些其他事情。我无能为力)

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

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