【问题标题】:Jquery : Simulate User click on Previous button/linkJquery:模拟用户单击上一个按钮/链接
【发布时间】:2015-03-08 15:18:54
【问题描述】:

大家好,

我正在使用以下代码将手动(通过用户点击)导航的分页表行转换为通过模拟用户点击上一个 - 下一个分页按钮自动前进。

我使用以下 setInterval 函数通过模拟鼠标单击“下一步”按钮“#tablepress-1_next”进入下一页,效果很好。

window.setInterval(function() 
    { 
        $('#tablepress-1_next').triggerHandler('click');
    }, 6000);

});

现在我想通过模拟用户点击“上一个”按钮来向后导航,但这对我来说有点太多了,因为我是 jquery 菜鸟。 anybuddy 可以帮助修改上述功能,以便在所有行都向前推进时模拟单击“上一个”按钮(“#tablepress-1_prev”),或者可以从第 1 页重新开始,而不是向后导航 2 向前。

如果是第 1 页,Tablepress 会为“上一页”按钮添加一个“禁用”类,如果是最后一页,则为“下一页”按钮添加一个“禁用”类。

Tablepress 是一个 wordpress 插件。

谢谢 问候, dkj

【问题讨论】:

    标签: jquery wordpress click


    【解决方案1】:

    如果有可用于此目的的 API,我不建议使用 DOM 事件处理导航。 DataTables API 提供了一些处理导航的功能,我添加了一个工作的jsfiddle 来演示该功能。基本上这些代码行是处理自动分页所必需的:

    // create the DataTable 
    var table = $('#example').DataTable();
    // retrieve the page information
    var info = table.page.info();
    // initialise the paging direction
    var direction= 'asc';
    
    // in case there are more than 1 pages of data, start pagination 
    if (info.pages > 1) {
      // start cycling the pages now
      window.setInterval(function() {
        if (direction=='asc') {
          table.page( 'next' ).draw( false );
          // switch directions if last page of data shown
          if (table.page()+1 == info.pages) direction= 'desc';
        } else {
          table.page( 'previous' ).draw( false );
          // switch directions if the first page of data is shown
          if (table.page() == 1) direction= 'asc';   
        }
      }, 2000);
    }
    

    祝你好运,如果这对你有用,请告诉我!

    【讨论】:

    • 您好,非常感谢您的帮助。 Tablepress 开发人员将代码格式化为
    • 您好,非常感谢您的帮助。 Tablepress 开发人员建议通过稍微扩展代码,将您的代码包含为 initComplete 回调的回调函数。看看这个Fiddle 但不幸的是那没有用。很奇怪,[DataTable API] (datatables.net/reference/type/DataTables.Settings) 格式和调用似乎是正确的。你可以试试这个工作吗?
    • 首先是关于链接的 SO 标记语法的简短说明:发布链接时,右方括号和左方括号之间不应有空格。您的版本的语法不正确,您尝试扩展 dataTable 而不是为完整事件添加处理程序。我更新了我的小提琴以合并建议的更改,看看here
    • 好的!那位 Gr8,很快就会回来报告,谢谢。
    • 好的。在 Tablepress 开发人员的帮助下。它似乎按预期工作。点击模拟是否可以像我们在 jquery 选择器上那样有一个淡入淡出。
    猜你喜欢
    • 1970-01-01
    • 2012-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多