幻灯片下方的未定义文本

Undefined text underneath the slideshow

本文关键字:未定义 文本 幻灯片      更新时间:2023-09-26

我创建了一个幻灯片,除了幻灯片下方的持久文本"未定义"之外,一切似乎都很好。我怎么可能摆脱它?请参阅下面的代码:

var variableslide = new Array()
//variableslide[x]=["path to image", "OPTIONAL link for image", "OPTIONAL text description (supports HTML tags)"]
variableslide[0] = ["https://farm2.staticflickr.com/1497/25124601229_90604fb064_b.jpg"]
variableslide[1] = ["https://farm2.staticflickr.com/1544/25196538220_755bfa57b8_b.jpg"]
variableslide[2] = ["https://farm2.staticflickr.com/1711/24865434833_8759084fed_k.jpg"]
variableslide[3] = ["https://farm2.staticflickr.com/1709/25373709692_bbbd7d1da8_k.jpg"]
variableslide[4] = ["https://farm2.staticflickr.com/1603/25466014396_475df73d82_b.jpg"]
variableslide[5] = ["https://farm2.staticflickr.com/1556/25373732262_7ce94cecb5_b.jpg"]
variableslide[6] = ["https://farm2.staticflickr.com/1537/25492103585_d2339eee89_b.jpg"]
variableslide[7] = ["https://farm2.staticflickr.com/1529/24861576224_d10ddae157_b.jpg"]
variableslide[8] = ["https://farm2.staticflickr.com/1466/25124597469_d8dec7aeac_b.jpg"]
//configure the below 3 variables to set the dimension/background color of the slideshow
var slidewidth = '1024px' //set to width of LARGEST image in your slideshow
var slideheight = '683px' //set to height of LARGEST iamge in your slideshow, plus any text description
var slidebgcolor = '#ffffff'
//configure the below variable to determine the delay between image rotations (in miliseconds)
var slidedelay = 3000
////Do not edit pass this line////////////////
var ie = document.all
var dom = document.getElementById
for (i = 0; i < variableslide.length; i++) {
  var cacheimage = new Image()
  cacheimage.src = variableslide[i][0]
}
var currentslide = 0
function rotateimages() {
  contentcontainer = '<center>'
  if (variableslide[currentslide][1] != "")
    contentcontainer += '<a href="' + variableslide[currentslide][1] + '">'
  contentcontainer += '<img src="' + variableslide[currentslide][0] + '" border="0" vspace="3">'
  if (variableslide[currentslide][1] != "")
    contentcontainer += '</a>'
  contentcontainer += '</center>'
  if (variableslide[currentslide][2] != "")
    contentcontainer += variableslide[currentslide][2]
  if (document.layers) {
    crossrotateobj.document.write(contentcontainer)
    crossrotateobj.document.close()
  } else if (ie || dom)
    crossrotateobj.innerHTML = contentcontainer
  if (currentslide == variableslide.length - 1)
    currentslide = 0
  else
    currentslide++
    setTimeout("rotateimages()", slidedelay)
}
if (ie || dom)
  document.write('<div id="slidedom" style="width:' + slidewidth + ';height:' + slideheight + '; background-color:' + slidebgcolor + '"></div>')
function start_slider() {
  crossrotateobj = dom ? document.getElementById("slidedom") : ie ? document.all.slidedom : document.slidensmain.document.slidenssub
  if (document.layers)
    document.slidensmain.visibility = "show"
  rotateimages()
}
if (ie || dom)
  start_slider()
else if (document.layers)
  window.onload = start_slider
<ilayer id="slidensmain" width=&{slidewidth}; height=&{slideheight}; bgColor=&{slidebgcolor}; visibility=hide>
  <layer id="slidenssub" width=&{slidewidth}; left=0 top=0></layer>
</ilayer>
<p align="center"><font face="Arial" size="-2">Free DHTML scripts provided by<br>
        <a href="http://www.dynamicdrive.com">Dynamic Drive</a></font>
</p>

variableslide[currentslide]似乎只有一个元素。您正在尝试获取返回undefinedvariableslide[currentslide][2]

在函数内部rotateimages删除这些行

if (variableslide[currentslide][2]!="")
contentcontainer+=variableslide[currentslide][2]

要更正代码,您需要检查函数中的值未定义而不是空字符串:

function rotateimages() {

现在,仅更改您可以看到行为的第一个元素适用于:

variableslide[0] = ["https://farm2.staticflickr.com/1497/25124601229_90604fb064_b.jpg", "https://farm2.staticflickr.com/1497/25124601229_90604fb064_b.jpg", "TEXT DESCRIPTION"]

并继续处理其余元素,例如:

variableslide[1] = ["https://farm2.staticflickr.com/1544/25196538220_755bfa57b8_b.jpg"]

因为就像您在代码中编写的那样,它们是可选字段。

我的片段:

var variableslide = new Array()
//variableslide[x]=["path to image", "OPTIONAL link for image", "OPTIONAL text description (supports HTML tags)"]
variableslide[0] = ["https://farm2.staticflickr.com/1497/25124601229_90604fb064_b.jpg", "https://farm2.staticflickr.com/1497/25124601229_90604fb064_b.jpg", "TEXT DESCRIPTION"]
variableslide[1] = ["https://farm2.staticflickr.com/1544/25196538220_755bfa57b8_b.jpg"]
variableslide[2] = ["https://farm2.staticflickr.com/1711/24865434833_8759084fed_k.jpg"]
variableslide[3] = ["https://farm2.staticflickr.com/1709/25373709692_bbbd7d1da8_k.jpg"]
variableslide[4] = ["https://farm2.staticflickr.com/1603/25466014396_475df73d82_b.jpg"]
variableslide[5] = ["https://farm2.staticflickr.com/1556/25373732262_7ce94cecb5_b.jpg"]
variableslide[6] = ["https://farm2.staticflickr.com/1537/25492103585_d2339eee89_b.jpg"]
variableslide[7] = ["https://farm2.staticflickr.com/1529/24861576224_d10ddae157_b.jpg"]
variableslide[8] = ["https://farm2.staticflickr.com/1466/25124597469_d8dec7aeac_b.jpg"]
//configure the below 3 variables to set the dimension/background color of the slideshow
var slidewidth = '1024px' //set to width of LARGEST image in your slideshow
var slideheight = '683px' //set to height of LARGEST iamge in your slideshow, plus any text description
var slidebgcolor = '#ffffff'
//configure the below variable to determine the delay between image rotations (in miliseconds)
var slidedelay = 3000
////Do not edit pass this line////////////////
var ie = document.all
var dom = document.getElementById
for (i = 0; i < variableslide.length; i++) {
  var cacheimage = new Image()
  cacheimage.src = variableslide[i][0]
}
var currentslide = 0
function rotateimages() {
  contentcontainer = '<center>'
  if (variableslide[currentslide][1] != undefined)
    contentcontainer += '<a href="' + variableslide[currentslide][1] + '">'
    contentcontainer += '<img src="' + variableslide[currentslide][0] + '" border="0" vspace="3">'
    if (variableslide[currentslide][1] != undefined)
      contentcontainer += '</a>'
      contentcontainer += '</center>'
      if (variableslide[currentslide][2] != undefined)
        contentcontainer += variableslide[currentslide][2]
        if (document.layers) {
          crossrotateobj.document.write(contentcontainer)
          crossrotateobj.document.close()
        }
  else if (ie || dom)
    crossrotateobj.innerHTML = contentcontainer
    if (currentslide == variableslide.length - 1)
      currentslide = 0
      else
        currentslide++
        setTimeout("rotateimages()", slidedelay)
}
if (ie || dom)
  document.write('<div id="slidedom" style="width:' + slidewidth + ';height:' + slideheight + '; background-color:' + slidebgcolor + '"></div>')
  function start_slider() {
    crossrotateobj = dom ? document.getElementById("slidedom") : ie ? document.all.slidedom : document.slidensmain.document.slidenssub
    if (document.layers)
      document.slidensmain.visibility = "show"
      rotateimages()
  }
if (ie || dom)
  start_slider()
  else if (document.layers)
    window.onload = start_slider