简体   繁体   English

更改图像和标题

[英]change image and caption

I am trying to work out a way to change the text that goes along with an image that is changed with javascript... 我正在尝试找出一种方法来更改随javascript更改的图像一起显示的文本...

var x = 0;
var images = new Array(".jpg", ".jpg", ".jpg", ".jpg", ".jpg");
var i = setInterval(auto, 10000);

function auto() {
    x++;
    if (x == images.length) x = 0;
    document.getElementById('bigImage').src = images[x];
}

function changeImage(img, imagetitle) {
    document.getElementById('bigImage').src = img;
    /* document.getElementById('mainimagetitle').innerHtml = imagetitle; */
}​​​

The commented part is how i suppose I could possibly change the text that goes with the image. 评论的部分是我想我可以如何更改图像附带的文本。 How do i code the html. 我如何编码的HTML。 Should i use a with the id mainimagetitle? 我应该将ID与mainimagetitle一起使用吗? If so, where and how do i add the different texts i want to show and hide? 如果是这样,我在哪里以及如何添加要显示和隐藏的不同文本?

As I see from your posting, this should do the job. 从您的帖子中可以看出,这应该可以完成工作。

<img id="bigImage" src="img1.jpg" alt="" />
<div id="mainimagetitle"></div>

Be sure to add a (filled) src tag or you will get strange results in IE. 确保添加一个(填充的)src标记,否则您将在IE中得到奇怪的结果。 As you start with the second image (x++ before the change) this will be no problem. 当您从第二个图像(更改之前的x ++)开始时,这将没有问题。 Happy accident I think. 我想是快乐的意外。 ;-) ;-)

// Edit: Of course any element will do as long as you use the right id. //编辑:当然,只要您使用正确的ID,任何元素都可以。 But you didn't tell us what html you use (xhtml/html5/...). 但是您没有告诉我们您使用什么html(xhtml / html5 / ...)。

You could potentially have another array that stores the captions for each of the images 您可能有另一个数组,用于存储每个图像的标题

var captions = ['Caption 1', 'Caption 2', ...];

Assuming the mainimagetitle is an id of a <p> element, you could do: 假设mainimagetitle<p>元素的ID,则可以执行以下操作:

function changeImage(img, imagetitle) {
     document.getElementById('bigImage').src = img;
     document.getElementById('mainimagetitle').innerText = imagetitle;
}​​​

You can see the full example, based on your code here . 您可以根据此处的代码查看完整的示例。

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

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