English 中文(简体)
住宿条件的变化
原标题:Variables in loop
carousel: function(){
            var $carouselCr = $( #carousel ),
                $tabCr = $( .carouselTabs , $carouselCr),
                $itemCr = $( .carouselContents , $carouselCr),
                tabAmount = (function(){
                    if($( a , $tabCr).length === $( .item , $itemCr).length){
                        return $( a , $tabCr).length;
                    }else{
                        throw "error: verschillend aantal tabs vs items";
                    }               
                })();

            var i = tabAmount;
            while(i--){                                     
                var item = $($( .item , $itemCr)[i]),
                    tab = $($( a , $tabCr)[i]);
                console.log(item, tab);
                $(tab).click(function(){
                    $( .item , $itemCr).hide();
                    $(item).show();
                })

            }

        }

各位可以看到,一米试图在每一表上附上一个点击事件,选择每个项目。 我做的是错的。 所有表格都指第一个项目。

如果在行程内一行<代码>$(项目、项目/项目/项目)[i],它将退回所有不同的项目,而不仅仅是第一个项目。

<>斯特罗内>简化超文本结构

<div id="carousel" class="block">
    <div class="carouselTabs">
        <a href="#">
        </a>
    <!-- repeating -->
    </div>
    <div class="carouselContents">                      
        <div class="item">
        </div>  
    <!-- repeating -->                  
    </div>
</div>
最佳回答

选择不产生新的变化范围。 你们需要单独设立点击手,并将任何需要扩大到这一职能。

 // creates the handler with the scoped item, and returns the handler
function create_handler(this_item) {
    return function () {
        $( .item , $itemCr).hide();
        $(this_item).show();
    };
}

var i = tabAmount;
var a_els = $( a , $tabCr);
var items = $( .item , $itemCr);
while (i--) {
    var item = items[i],
        tab = a_els[i];
    $(tab).click( create_handler(item) );
}

还指出,在座右翼中,你不应做甄选。 它一度躲在 lo门外,就象我前面所说的那样在 lo中。


看来,从原来的问题看,对法典做了一些改动。 我照此重写法典:

carousel: function(){
    var $carouselCr = $( #carousel ),
        $tabCr = $( .carouselTabs , $carouselCr),
        $itemCr = $( .carouselContents , $carouselCr),
        $items = $( .item , $itemCr),
        $a_els = $( a , $tabCr);

    if($a_els.length !== $items.length)
        throw "error: verschillend aantal tabs vs items";

    $a_els.each(function(i) {
        $(this).click(function() {
            $items.hide();
            $items.eq(i).show();
        });
    });
}

现在,每张<代码>.click(>)的手稿都指独一无二的<代码>i,即现行<代码>的索引(a_els在校正中的元件)。

So for example when a click happens on the $a_els at index 3, $items.eq(i).show(); will show the $items element that is also at index 3.


另一种做法是利用活动代表团,在你把手提放在集装箱内,并提供一个甄选人,以确定是否应援引手稿。

如果您重新使用j Query 1.7或以后,请使用.on(......

carousel: function(){
    var $carouselCr = $( #carousel ),
        $tabCr = $( .carouselTabs , $carouselCr),
        $itemCr = $( .carouselContents , $carouselCr),
        $a_els = $( a , $tabCr),
        $items = $( .item , $itemCr);

    if($a_els.length !== $items.length)
        throw "error: verschillend aantal tabs vs items";

    $tabCr.on( click , a ,function() {
        var idx = $a_els.index( this ); // get the index of the clicked <a>
        $items.hide();
        $items.eq(idx).show(); // ...and use that index to show the content
    });
}

或在j Query 1.7之前,请使用.delegate(......

carousel: function(){
    var $carouselCr = $( #carousel ),
        $tabCr = $( .carouselTabs , $carouselCr),
        $itemCr = $( .carouselContents , $carouselCr),
        $a_els = $( a , $tabCr),
        $items = $( .item , $itemCr);

    if($a_els.length !== $items.length)
        throw "error: verschillend aantal tabs vs items";

    $tabCr.delegate( a , click ,function() {
        var idx = $a_els.index( this ); // get the index of the clicked <a>...
        $items.hide();
        $items.eq(idx).show(); // ...and use that index to show the content
    });
}

This way there s only one handler bound to the $tabCr container. It check to see if the item clicked matches the a selector, and if so, it invokes the handler.

If there are other elements in between the <a>...</a> elements or the <div class="item">...</div> elements so that the indices don t naturally match up, we d need to tweak the .index() call just a bit.

问题回答

to simplify your code and to make it more performant you can use a delegate method

$( .carouselTabs ,  #carousel ).delegate( a ,  click , function(){

  var ind = $( .carouselTabs a ).index(this);
  $( .item ,  #carousel ).hide().eq(ind).show();

  return false;
});




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

热门标签