jquery - 在 jquery UI 对话框中创建动态上一个下一个按钮

标签 jquery jquery-ui button dialog

我在这个网站和网络上多次搜索过这个内容

是否可以在 jquery 对话框中动态创建上一个和下一个按钮?

我有一个链接列表,我想单击一个链接并打开一个对话框。在该对话框中将有一个上一个和下一个按钮,单击该按钮将关闭当前对话框并在另一个对话框中打开列表中的上一个或下一个项目,依此类推。

类似这样的事情

HTML

<ul id="dialoglist">
  <li>
    <a href="list1.html">
  </li>
  <li>
    <a href="list2.html">
  </li>
  <li>
    <a href="list3.html">
  </li>
  <li>
    <a href="list4.html">
  </li>
</ul>

jQuery

$("ul#dialoglist a").click(function(){
    var link = $(this).attr('href') + " #content";
    var box = $('<div></div>').load(link).dialog({
        buttons: {
            "prev": function() {
                 $(this).dialog('close');
                 //open previous dialog
             },
             "next": function() {
                 $(this).dialog('close');
                 //open next dialog
             }
        }
    });
    $(box).dialog('open');
    return false;
});

谢谢

最佳答案

这样的事情应该有效:

$("ul#dialoglist a").click(function(){
    var a = $(this);
    var link = a.attr('href') + " #content";

    // move button creation here so we can dynamically build 
    // the button hash:

    var aParent = a.parent();
    var buttons = {};
    var prevLi = aParent.prev();
    var nextLi = aParent.next();

    if(prev.length > 0){
      buttons.prev = function(){
        // not sure if this is in the corret scope here - you may need to select by id
        $(this).dialog('close');
        $('a', prevLi).click();
      };
    }

    if(next.length > 0){
      buttons.next = function(){
        / not sure if this is in the corret scope here - you may need to select by id
        $(this).dialog('close');
        $('a', nextLi).click();
      };
    }

    var box = $('<div></div>').load(link).dialog({
        'buttons': buttons
    });
    $(box).dialog('open');
    return false;
});

由于您只是触发上一个/下一个链接上的单击事件,因此您不必担心手动打开对话框。

但是...为什么要打开新对话框而不是使用对话框小部件 api 直接设置内容?

关于jquery - 在 jquery UI 对话框中创建动态上一个下一个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3965495/

相关文章:

android - 如何使按钮指向右或左?

ios - 这是 iOS UITableView 的示例吗?

javascript - 从内容可编辑 div 中删除?

javascript - 如何使用 jQuery 插入多个 HTML 元素

javascript - 悬停时更改底部边框的颜色

javascript - 拖动开始时更改可拖动元素的大小

jquery - 使用 cshtml 中返回的 JQuery FOREACH 行切换可见性

javascript - href ="#"中的 event.preventDefault() 不会阻止跳转到 IE 和 Firefox 中的页面顶部

javascript - jQuery 是否可以将()绑定(bind)到alert()事件,这样我就可以绕过默认消息框

javascript - 如何让按钮不获得焦点?