使用jquery切换通过ajax加载的元素

使用jquery切换通过ajax加载的元素,jquery,ajax,toggle,Jquery,Ajax,Toggle,我有一个.php,它通过ajax加载到一个div中。在这个.php中,我有一个”; echo“”; echo$t反馈; 回声“”; jquery: <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $('.ex

我有一个.php,它通过ajax加载到一个div中。在这个.php中,我有一个
”;
echo“”;
echo$t反馈;
回声“

”;
jquery:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){ 
$('.exp').on('click', function (e) {
    e.preventDefault(); // stop href()
    $(this).next().toggle();      
});
    });
</script>

$(文档).ready(函数(){
$('.exp')。在('click',函数(e)上{
e、 preventDefault();//停止href()
$(this.next().toggle();
});
});

$t反馈是字符串-我已检查以确保它不是空的。

在通过AJAX加载锚和段落之前,您可能正在执行第二个代码段中的代码。在此阶段,没有与
.exp
匹配的元素,因此集合
$('.exp')
为空

解决方案是委托给已经存在的祖先:

$(document).on('click', '.exp', function (e) {
    e.preventDefault(); // stop href()
    $(this).next().toggle();      
});

@OmairVaiyani我在任何地方都看不到
查看说明
链接。抱歉,链接错误,更新了:谢谢!效果很好
$(document).on('click', '.exp', function (e) {
    e.preventDefault(); // stop href()
    $(this).next().toggle();      
});
Please try this code hope it helps you......

$(document).delegate('.exp','click', function (e) {
    e.preventDefault(); // stop href()
   if($(this).next().css('display')=='block'){
    $(this).next().hide();
}else{
    $(this).next().show();
     }   
});

OR

$(document).on('click', '.exp', function (e) {
    e.preventDefault(); // stop href()
    if($(this).next().css('display')=='block'){
    $(this).next().hide();
}else{
    $(this).next().show();
     }      
});