English 中文(简体)
在休息期间,如何根据指数价值确定的条件安排活动
原标题:Under loop, how to assign event depending on the condition based on index value

我有50个小区,我正在根据目前的内容安排活动。 然而,我希望根据情况安排活动。

For example, divs are arranged one behind another, just like a photo stack. I want user to first hit first div, then only they should able to hit second div then third div and so on till they reach the end of divs.

由于以下代码用户可打碎机,并采取行动。 但行动需要逐一进行。 之后一分四。 请建议解决办法。

var flag=0;
var flipImage=document.querySelectorAll( .shadow );  
   for(j=0; j<flipImage.length; j++){
   var currentFlipImage=flipImage[j];
      if(j==flag){
      flag++; 
      currentFlipImage.addEventListener( click ,flipDown,false);
     }
  }
最佳回答

也许像:

var _handler = null;
var images = document.querySelectorAll( .shadow );
var length = images.length;

function attachHandler(index) {
    if(_handler) {
        // remove the click handler from the previous image
        images[index-1].removeEventListener( click , _handler, false);
        _handler = null;
    }
    if(index < length) {
        var handler = function(event) {
            flipDown(event);
            attachHandler(index+1); // add the click handler to next image
        }
        images[index].addEventListener( click , handler, false);
        _handler = handler;  // capture reference for removal
    }
}

attachHandler(0);

这只能产生并在必要时增加活动听众。

重要的附带说明: <代码>querySelectorAll()的回报率为,live<>。 这意味着,在您称之为<代码>length时,该清单将重新评价(这些内容将再次查询),从而增加履约费用。 因此,从来不使用<代码>list.length,载于for。 页: 1

rel=“nofollow”>Working DEMO

If you want to make it going in a loop, just remove the if statement (keep the body of course) and do

index = index % length;
问题回答

然后按顺序增加点击事件。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Test</title>
<script type="text/javascript">
function div_click(event)
{
  var div_clicked = event.currentTarget,
      next_sequence = parseInt(div_clicked.id.split("_")[1]) + 1,
      next_div = document.getElementById("mydiv_" + next_sequence);
  //alerts are just for testing here
  alert(div_clicked.id);
  alert(next_div.id);

  div_clicked.removeEventListener( click ,div_click,false);
  next_div.addEventListener( click ,div_click,false);
}

window.onload = function()
{
  var first_div = document.getElementById("mydiv_0");
  first_div.addEventListener( click ,div_click,false);
};
</script>
</head>
<body>
<div id="mydiv_0">Click 0</div>
<div id="mydiv_1">Click 1</div>
<div id="mydiv_2">Click 2</div>
<div id="mydiv_3">Click 3</div>
<div id="mydiv_4">Click 4</div>
<div id="mydiv_5">Click 5</div>
</body>
</html>

值得射击?

为此:

var flipImage = document.querySelectorAll( .shadow ), current = 0;
function flipDown(){
   current++;
   current = (current > flipImage.length)? 0 : current;
   for(j = 0; j < flipImage.length; j++){
      flipImage[j].style.zIndex = (j == current)? 2 : 1;
   }
}
window.onload = function(){
  for(j = 0; j < flipImage.length; j++){
    flipImage[j].onmouseclick=flipDown;
  }
}

当你拥有与同一事件手持平的武断内容时,处理共同父母的所有事件通常胜于指派处理人处理每一事项。

这一工作旨在开展重点活动或开展非布置活动,但对于触动、 mo动和关键板的活动是有益的。

通常比从任意因素出发,根据经常开关板手递的条件协调行动。





相关问题
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.