简体   繁体   English

单击图像到div时如何显示文本

[英]How to show text when clicking on an image to a div

I want to post an image of 2 DIVs, separated by a black line , the one on the left have 4 images and what I want is: 我想发布一张由黑线分隔的2个DIV的图像,左侧的一个有4个图像,我想要的是:

When I click on any of the buttons it will reveal a text in the right DIV and when I press another image it reveals, too, another text. 当我单击任何一个按钮时,它将在右侧的DIV中显示一个文本,而当我按下另一个图像时,它也将显示另一个文本。 I have no idea how I can do that, that's why I'm here asking for help. 我不知道该怎么做,这就是为什么我在这里寻求帮助。

If you have any ideas about the subject, like what I must use, or sources that you can find because I didn't find anything related. 如果您对这个主题有任何想法,例如我必须使用的东西,或者因为找不到相关内容而可以找到的资源。

I've searched about this in JavaScript revealing a text from a button, but nothing that could help me. 我已经在JavaScript中搜索过此内容,它显示了按钮上的文本,但没有任何帮助。

For your better understanding i will show you how is the final result, how it should look. 为了使您更好地理解,我将向您展示最终结果如何,外观如何。 http://prntscr.com/7krf5h http://prntscr.com/7krf5h

Of course without the arrows pointing, and the image in the right div, it will disappear, i just need the text and show the text, but i think u understand the content. 当然,如果没有箭头指向,并且右边的div中的图像会消失,我只需要文本并显示文本即可,但是我认为您理解其中的内容。

You need to use jquery in order to achieve what you want. 您需要使用jquery来实现所需的功能。

Also, you need to change the text of the div when you click on another div tag. 另外,当您单击另一个div标签时,您需要更改div的文本。 For this, you need to user inner html. 为此,您需要使用内部html。

Here is the sample code, you can try it. 这是示例代码,您可以尝试一下。

$(document).ready(function(){
$("name_of_image_div_tag_here").click(function(){
    alert("The image was clicked.");
    document.getElementById("name_of_text_div_tag_here").innerHTML = "Sample text to display";
    });
});

It looks like you're trying to setup a slideshow of some sort. 您似乎正在尝试设置某种幻灯片放映。 One way you can do it is to create two lists, a hidden one for the slide data and another visible one for the list of slides on the left-hand side. 一种方法是创建两个列表,一个隐藏的列表用于幻灯片数据,另一个可见的列表用于左侧的幻灯片列表。 Then, you can grab the necessary data from the hidden list based on index. 然后,您可以根据索引从隐藏列表中获取必要的数据。 Here's an example fiddle that will highlight what I mean: http://jsfiddle.net/ethueuhj/1/ (in the fiddle, you can change the data-bg attribute to a computer background, tablet background, et al). 这是一个示例小提琴,它将突出我的意思: http : //jsfiddle.net/ethueuhj/1/ (在小提琴中,您可以将data-bg属性更改为计算机背景,平板电脑背景等)。

$(function() {
    function setSlide(index) {
        var slide = $('.slide-data li').eq(index);
        $('.slide-container').css({ "background-image": "url('" + slide.data('bg') + "')" });
        $('.slide-container').html(slide.html());
    }

    $('.slides li').click(function() {
        setSlide($(this).index());
    });

    // Default slide
    setSlide(0);
});

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

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