Undefined text underneath the slideshow

时间:2016-03-04 18:10:34

标签: javascript jquery

I have created a slideshow and all seems nice, apart from the persistent text 'undefined' underneath the slideshow. How can I possibly get rid of it? Please see the code below:

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>

2 个答案:

答案 0 :(得分:0)

variableslide[currentslide]似乎只有一个元素。您正试图让variableslide[currentslide][2]返回undefined

rotateimages内部功能删除这些行

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

答案 1 :(得分:0)

要更正代码,您需要在函数中检查值undefined而不是空字符串:

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