加载AJAX后,Readmore.js无法正常工作

时间:2017-06-22 14:18:44

标签: jquery ajax

我目前正在使用Readmore.js插件,但似乎无法使用AJAX内容。

<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调用中调用函数,但我只是觉得它会工作lol。

$(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();
});

1 个答案:

答案 0 :(得分: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>