Javascript Colorbox:调用 colorbox.close

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5436055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:11:36  来源:igfitidea点击:

Colorbox: Calling colorbox.close

javascriptjqueryjquery-pluginscolorbox

提问by Adam

I'm working on a project that uses the velocity templating system, so the $ character is reserved, and cannot be used in javascript variable names.

我正在开发一个使用速度模板系统的项目,所以 $ 字符是保留的,不能在 javascript 变量名中使用。

As such, I have to prefix jQuery variables and methods with jQuery, rather than $, e.g. jQuery(document).ready(function() {}); as opposed to $(document)ready(function(){});

因此,我必须在 jQuery 变量和方法前加上 jQuery,而不是 $,例如 jQuery(document).ready(function() {}); 与 $(document)ready(function(){}) 不同;

This is ordinarily fine, but in this case I am using colorbox.

这通常很好,但在这种情况下,我使用的是 colorbox。

My code to call colorbox works fine, and looks like this:

我调用 colorbox 的代码运行良好,如下所示:

jQuery(document).ready(function () {
    jQuery("#addUser").colorbox({
        href:"add",
        width:"500px",
        onClosed: function (message) {
            dataTable.refresh(jQuery("#ajaxResult").text(message));
        }
    })

    ...
})

I have a link inside the colorbox that I want to attach the colorbox.close method to, but when I click the link, I get this error:

我想将 colorbox.close 方法附加到 colorbox 中的一个链接,但是当我单击该链接时,出现此错误:

Uncaught TypeError: Cannot call method 'close' of undefined

未捕获的类型错误:无法调用未定义的方法“关闭”

This is my code:

这是我的代码:

jQuery(document).ready(function () {
    jQuery("a").click(function() {
        jQuery.colorbox.close("User added succesfully");
    });

    ...
})

Can anybody tell me why I cannot close the colorbox?

谁能告诉我为什么我不能关闭颜色框?

By the way, the X that comes with colorbox still works to close it.

顺便说一句,colorbox 自带的 X 仍然可以关闭它。

采纳答案by Adam

The easiest way to solve this for me was to store jQuery.colorbox as a variable in the global namespace. Yucky, but it works.

对我来说解决这个问题的最简单方法是将 jQuery.colorbox 作为变量存储在全局命名空间中。恶心,但它有效。

Here is what I put in the parent window:

这是我放在父窗口中的内容:

jQuery(document).ready(function () {
    colorbox = jQuery.colorbox;

    ...
})

Then this is how I call it:

然后我就是这样称呼它的:

jQuery(document).ready(function () {
    jQuery("a").click(function() {
        colorbox.close("User added succesfully");
    });

    ...
})

回答by Chris Shouts

jQuery("#addUser").colorbox.close("User added succesfully");

Also, you should be able to use the $syntax for jQueryif you choose by using external javascript files <script type="text/javascript" src="my_script_file.js" />or escaping the $like \$.

此外,你应该能够使用的$语法jQuery,如果您选择使用外部JavaScript文件<script type="text/javascript" src="my_script_file.js" />逃避$类似\$

回答by joan16v

This worked for me:

这对我有用:

jQuery().colorbox.close();

回答by sushil bharwani

can you try writing jQuery.fn.colorbox.close("User added succesfully"); instead of jQuery.colorbox.close("User added succesfully");

你可以试试写 jQuery.fn.colorbox.close("用户添加成功"); 而不是 jQuery.colorbox.close("用户添加成功");