English 中文(简体)
限制阵列(Javascript)
原标题:Get a limit on arrays (Javascript)

我提出了一个问题,在我自己的灯塔中设置一个限制。

    <script>


var imagenumber = 0;

    function btnleft(){
        load = imagenumber-=1;
        document.getElementById( lightboxcontent ).innerHTML=imagelist[load];

        }

function btnright(){
    load = imagenumber+=1;
    if (load==undefined){load=imagenumber-=1}
    document.getElementById( lightboxcontent ).innerHTML=imagelist[load];
    }
</script>

然后是阵列

var imagelist=new Array(); // regular array (add an optional integer
imagelist[0]="image1.jpg";       // argument to control array s size)
imagelist[1]="image2.jpg";
imagelist[2]="image3.jpg";

When I click more then 3 times on the next button I got the error-message "undefined". How should I do to get a limit on my arrays?

最佳回答

提 交

 function btnleft(){
    var load = imagelist[imagenumber-=1];
    if (load) // imagenumber in array boundaries
        document.getElementById( lightboxcontent ).innerHTML = load;
    else
        imagenumber = 0;
 }
 function btnright(){
    var load = imagelist[imagenumber+=1];
    if (load) // imagenumber in array boundaries
        document.getElementById( lightboxcontent ).innerHTML = load;
    else
        imagenumber = imagelist.length-1;
 }

然而,在 Java文中,<代码>Array的大小并不有限,更像(有限)清单。 您很难对其长度设定限制——从中说不与https://developer.mozilla.org/en/Javagust/Reference/Global_Objects/Array” rel=“nofollow”> 施工或,其编号理由只是初步确定。

您可使用length Property ,其中一阵列,以检查您的指数在阵列中:i >= 0 && i < arr.gth 。 我的法典只是检查了该指数中是否有一个项目(因为你的第二个职能似乎也打算这样做),并用其他方式修改了该指数。

问题回答

我假定,点击“next button”指的是btnright(功能。

如果情况如此,你正在测试<代码>未界定<>/代码>的错误价值。 您可以重新履行以下职能:

function btnright(){
  load = imagenumber += 1;
  // Test the value at the index of the array, not your index variable.
  if (imagelist[load] === undefined) {
    load = imagenumber-= 1;
  }
  document.getElementById( lightboxcontent ).innerHTML = imagelist[load];
}

从理论上讲,这仍然是最好的。 页: 1 不需要变数,因为其价值总是重复<代码>image /code>。 您可以证明这种职能:

function btnright() {
  // If we have a new array value do something.
  if (imagelist[imagenumber + 1] !== undefined) {
    // Increment the index and load the new image.
    document.getElementById( lightboxcontent ).innerHTML = imagelist[++imagenumber];
  }
}

function btnleft() {
  // If we re not on the first image do something.
  if (imagenumber !== 0) {
    // Decrement the index and load the new image.
    document.getElementById( lightboxcontent ).innerHTML = imagelist[--imagenumber];
  }
}




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签