2017-06-22 190 views
0

我目前使用的是Readmore.js插件,似乎无法使其与AJAX内容一起使用。加载AJAX后,Readmore.js不起作用

<td><div class="more"><?php echo $row['book_desc'];?></div></td> 

JS文件

$(document).ready(function() { 
    var url = "/func/admin/books.php"; 
    function loadBooks() { 
     $.ajax({ 
      url: url, 
      type: "post", 
      data: { 
       action: "load" 
      }, 
      dataType: "html", 
      success: function(data) { 
       $("#books").hide().html(data).fadeIn(); 
      } 
     }); 
    } 
    loadBooks(); 
    $(".more").readmore(); 
}); 

我也包括jQuery和Readmore.js文件。

我已经完成了下面的代码(阅读更多的链接出现,但显然没有削减.more的内容)。我不应该一直在调用ajax调用函数,但我只是认为它会工作大声笑。

$(document).ready(function() { 
    var url = "/func/admin/books.php"; 
    function loadBooks() { 
     $.ajax({ 
      url: url, 
      type: "post", 
      data: { 
       action: "load" 
      }, 
      dataType: "html", 
      success: function(data) { 
       $("#books").hide().html(data).fadeIn(); 
       $(".more").readmore(); 
      } 
     }); 
    } 
    loadBooks(); 
}); 
+0

可以共享HTML –

+0

@NidhinChandran HTML上面已经包含在内。如果你的意思是脚本标签,我会添加它们。 – devgirl

回答

0

试试这个。我用mockajax插件来模拟ajax请求。检查AJAX功能

var data = "From this distant vantage point, the Earth might not seem of any particular interest. But for us, it's different. Consider again that dot. That's here. That's home. That's us. On it everyone you love, everyone you know, everyone you ever heard of, every human being who ever was, lived out their lives. The aggregate of our joy and suffering, thousands of confident religions, ideologies, and economic doctrines, every hunter and forager, every hero and coward, every creator and destroyer of civilization, every king and peasant, every young couple in love, every mother and father, hopeful child, inventor and explorer, every teacher of morals, every corrupt politician, every superstar, every supreme leader, every saint and sinner in the history of our species lived there – on a mote of dust suspended in a sunbeam. From this distant vantage point, the Earth might not seem of any particular interest. But for us, it's different. Consider again that dot. That's here. That's home. That's us. On it everyone you love, everyone you know, everyone you ever heard of, every human being who ever was, lived out their lives. The aggregate of our joy and suffering, thousands of confident religions, ideologies, and economic doctrines, every hunter and forager, every hero and coward, every creator and destroyer of civilization, every king and peasant, every young couple in love, every mother and father, hopeful child, inventor and explorer, every teacher of morals, every corrupt politician, every superstar, every supreme leader, every saint and sinner in the history of our species lived there – on a mote of dust suspended in a sunbeam. From this distant vantage point, the Earth might not seem of any particular interest. But for us, it's different. Consider again that dot. That's here. That's home. That's us. On it everyone you love, everyone you know, everyone you ever heard of, every human being who ever was, lived out their lives. The aggregate of our joy and suffering, thousands of confident religions, ideologies, and economic doctrines, every hunter and forager, every hero and coward, every creator and destroyer of civilization, every king and peasant, every young couple in love, every mother and father, hopeful child, inventor and explorer, every teacher of morals, every corrupt politician, every superstar, every supreme leader, every saint and sinner in the history of our species lived there – on a mote of dust suspended in a sunbeam."; 
 
$(document).ready(function() { 
 

 
    $.mockjax({ 
 
    url: 'data.json', 
 
    responseText: data, 
 
    responseTime: 1500 
 
    }); 
 

 
    $.ajax({ 
 
    url: 'data.json', 
 
    dataType: 'html', 
 
    success: function(data) { 
 
     $(".more").html(data).readmore({ 
 
     speed: 500, 
 
     collapsedHeight: 50 
 
     }); 
 
    } 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
 
<script src="https://fastcdn.org/Readmore.js/2.1.0/readmore.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-mockjax/1.6.2/jquery.mockjax.min.js"></script> 
 
<table> 
 
    <tr> 
 
    <td> 
 
     <div class="more"> 
 
     Some content Some content Some content Some content Some content Some content Some content Some content Some content Some content Some content Some content Some content Some content Some content Some content Some content 
 
     </div> 
 
    </td> 
 
    </tr> 
 
</table>