使用jQuery在Ajax结果中使用选择器问题

时间:2014-03-11 13:49:47

标签: javascript jquery html css ajax

我有一个包含一些立方体的页面...
当用户单击多维数据集时,多维数据集的宽度和高度将发生变化,相关内容将使用Ajax进行获取 Ajax结果是另一个html页面本身,它有一些jQuery命令 请查看result页面:

HTML

<div id="wrapperProducts">
<div class="products" id="mainContent">

  </div><!-- # mainContent -->
  <div class="products" id="rightPanel">

  </div><!-- # rightPanel -->
  <div class="products" id="footerPanel">

    <div class="arr" id="arrowRightFooter">
       <img src="images/arrows/light/arrowRight.png" width="30" height="30"  alt=""/>
     </div><!-- # arrowRightFooter -->

    <div id="thumbWrapper">
        <div class="thumb" id="t1">

        </div><!-- # t1 -->
        <div class="thumb" id="t2">

        </div><!-- # t2 -->
        <div class="thumb" id="t3">

        </div><!-- # t3 -->
        <div class="thumb" id="t4">

        </div><!-- # t4 -->
        <div class="thumb" id="t5">

        </div><!-- $ t5 -->
        <div class="thumb" id="t6">

        </div><!-- # t6 -->
        <div class="thumb" id="t7">

        </div><!-- # t7 -->
        <div class="thumb" id="t8">

        </div><!-- # t8 -->
        <div class="thumb" id="t9">

        </div><!-- # t9 -->
     </div><!-- # thumbWrapper-->

     <div class="arr" id="arrowLeftFooter">
       <img src="images/arrows/light/arrowLeft.png" width="30" height="30"  alt=""/>
     </div><!-- # arrowLeftFooter -->

  </div><!-- # footerPanel -->

CSS:

div#logo div#wrapperProducts {
    width:100%;
    height:100%;
    background:url(../images/27.jpg) no-repeat;

}
div#logo div#wrapperProducts div#mainContent {
    left:0;
    top:60px;
    width:80%;
    height:70%;
    float:left;
    /*background-color:red;*/   
}
div#logo div#wrapperProducts div#rightPanel {
    right:-10px;
    top:0;
    width:20%;
    height:100%;
    float:right;
    position:absolute;
    /*background-color:blue;*/  
}
div#logo div#wrapperProducts div#footerPanel {
    bottom:0;
    left:0;
    position:absolute;
    width:80%;
    height:30%;
    /*background-color:#096;*/
}
div#logo div#wrapperProducts div#thumbWrapper {
    width:90%;
    height:100%;
    margin:auto;
    margin-right:5%;
    overflow:hidden;
    white-space:nowrap;
    /*background-color:;*/
}
div#logo div#wrapperProducts div#thumbWrapper .thumb {
    width:170px;
    height:100%;
    margin-right:2px;
    right:0;
    display:inline-block;/*inline-block*/
    position:relative;
    direction:rtl;
    background: linear-gradient(rgba(255,255,255, 0.2), rgba(255,255,255, 0) );
    background-color:transparent;

}
div#logo div#wrapperProducts div#footerPanel div.arr {
    position:absolute;
    width:30px;
    height:30px;
    cursor:pointer;
}
div#logo div#wrapperProducts div#footerPanel #arrowRightFooter {
    right:0;
    bottom:50%; 
}
div#logo div#wrapperProducts div#footerPanel #arrowLeftFooter {
    left:0;
    bottom:50%; 
}


div#logo是主要页面元素

JS:

$(document).ready(function(e) {
  /*"use strict";*/

$('#logo').css({'background':'none'});
  $('#arrowRightFooter').click( function(){


        console.log('The click button work fine');
        $('.thumb').animate({left: '+=170px' }, 1500);

    });
});

这一行是#logo

main page:的结构

HTML

<div class="parts" id="part1">

    <div class="third" id="left">

      <div class="mostatil radif1" id="logo">

            <div class="imgWrapper">
              <img src="images/icons/new/basketStore.png" width="120" height="90"  alt=""/>
            </div>
      </div>
    <div>
</div>

CSS:

body {
    background:url(../images/background.png) no-repeat;
    background-color:rgb(24,1,83);
    font-family:"B Koodak", Tahoma;
}

div.parts {
    width:620px;
    height:800px;
    position:absolute;
}

div#part1 {
    margin-left:150px;
}

div.third {
    width:510px;
    height:640px;
    margin-top:100px;
}

div#left {
    margin-left:55px;   
}

div.third div.mostatil {
    width:250px;
    height:120px;   
}

div.third#left div.radif1 {
    margin-top:0px; 
}

div#logo {
    background-color:rgb(214,164,1);
    position:absolute;
    cursor:pointer;
}

和* jQuery:*

/*/-------------  C L I C K    ON    DIV # L O G O  --------------/*/
$(document).on('click', 'div.mostatil#logo', function(){

        $('div.moraba, div.mostatil').not('#logo').delay(500).fadeOut('fast');/**/

        var detail = {};
        detail.id               = $(this).attr('id');
        detail.class           = $(this).attr('class').substr(7);
        $(this)
                .animate({
                    width:         WinWidth,
                    height:           WinHeight,
                    marginTop:   -60,
                    marginLeft:  -210
                }, 100, 'easeOutQuint', false)
                .data({ id: detail.id, class: detail.class})
                .css({'z-index':500, 'cursor':'default'})
                .transition({rotateY:'360deg'}, 1000,'snap');

        $('body').css('overflow','hidden');

        $.ajax({
            type:'POST',
            url:"pages/Ajax/products.php",
            beforeSend: function() {
                $('#logo').html('LOADING; PLEASE WAIT...')
            },
            statusCode: {
                404: function(){
                    $(this).html('Page not found');
                }
            },
            success: function(result) {
                $('#logo').html(result);
            }
        });//Ajax


});

当我选择结果页面的其他元素时,如wrapperProductmainContent等等,animate方法工作正常,但只是在$('.thumb')中,点击后没有结果在arrow divs ...
我认为我的CSS有问题,可能是whitespace:nowrap,因为这是我第一次使用这个命令...
我相信这是因为我误解了CSS概念...... 请指导我写作方式并帮助弄清楚我的错... 提前致谢...

1 个答案:

答案 0 :(得分:1)

实际上AJAX ...有问题 选择器没有任何问题,

 您已经定义了应用程序向服务器发送Ajax请求,并且每次点击HTML时再次将div.mostatila#logo结果插入此div ... !!! 所以,
当您点击页面中的每个位置时,您实际上是点击#logo ...

您必须将Ajax请求限制为某种情况,例如:

        if ( $(this).css('width') == '250px' ) {
            $.ajax({
                type:'POST',
                url:"pages/Ajax/products.php",
                beforeSend: function() {
                    $('#logo').html('LOADING; PLEASE WAIT...')
                },
                statusCode: {
                    404: function(){
                        $(this).html('Page Not Found...');
                    }
                },
                success: function(result) {
                    $('#logo').html(result).css('background','none');
                }
            });//Ajax
        }
        else {
            return; 
        }

然后,当#logowidth:250px时,Ajax请求将被发送...
我认为这是问题...