javascript - 优化 colorbox 并添加额外的 jquery

标签 javascript jquery jquery-plugins colorbox

我有两个问题

  1. 我正在尝试打开 jQuery 颜色框,但速度非常慢。原因是我试图从不同的页面获取 html 内容(我无法使用 iframe,因为我只需要此页面的一部分)。以下代码有效,但单击按钮后需要一些时间:

    $(document).ready(function() {
        $(".cart-link a").click(function(event) {
            $(this).colorbox.close();
        });
    
    
        $(".rest-menuitem a").click(function(event) {
            event.preventDefault();
            var result = null;
            var sURL = $(this).attr("href");
            $.colorbox({
                html: function() {
                    $.ajax({
                        url: sURL,
                        type: 'get',
                        dataType: 'html',
                        async: false,
                        success: function(data) {
                            result = data;
                        }
                    });
                    return $(result).find('.product');
                },
                width: '650px',
                height: '10px',
                onComplete: function() {
                    $(this).colorbox.resize();
                }
            });
    
        });
    
    });
    

    我想知道是否有其他方法可以做到这一点。我不介意是否弹出颜色框然后需要时间来加载内容。上述版本可以在此网址( http://delivery3.water-7.com/index.php/restaurants/manufacturers/3/Barcelona-Restaurant-&-Winebar/products )找到。

  2. 当用户单击“添加到购物车”时,我还尝试关闭颜色框。但由于某种原因它没有被触发。当我点击“添加到购物车”时,不会触发 $(".cart-link a").click 。有没有特殊的方法将 jquery 添加到 colorbox 内容?

最佳答案

试试这个:

$(".rest-menuitem a").colorbox({
    href: function(){ 
        return $(this).attr('href') + ' .products';
    },
    width: '650px',
    height: '10px',
    onComplete: function() {
        $(this).colorbox.resize();
    }
});

ColorBox 使用 jQuery 的 load() 方法进行 ajax 处理,因此您只需将所需的选择器添加到链接的 href 中即可。

关于javascript - 优化 colorbox 并添加额外的 jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8967899/

相关文章:

javascript - jQuery 不响应 AJAX 加载的内容

.net - 如何将 jQuery DateTimePicker 添加到 Visual Studio 中的项目中

javascript - 无法使用 nconf 加载配置 json 文件

javascript - 缩短显示的 URL,保留开头和结尾(Firebug 'Net' 面板样式)

javascript - 在 HTTP 网站中使用 HTTPS url

jquery - JQuery Lightslider 幻灯片水平居中

jquery - 如何在背景图像上进行过渡?

javascript - Bable 只生成 js 文件(在 dist 文件夹中),而不生成项目目录中的其他文件(如 html、css ..)。我正在使用 "build":"babel source -d dist"

javascript - 如何将更改后的值存储到 JavaScript 对象中?

jquery - 幻灯片切换 jQuery 函数