1
4

3 回答 3

3
于 2012-11-15T14:42:18.680 回答
1
$('a#yourLinkId').click(function(e){
  // prevents default behaviour
  e.preventDefault();

  // your stuff
  $.colorbox({opacity:0.3, href:"ext/popup.php"}); 

});
于 2012-11-15T14:42:14.420 回答
1

没问题。您可以在 Stackoverflow 中询问有关 JQuery 的任何问题,我们会尽力提供帮助。

关于你的问题。我认为您想使用 JQuery 在ColorBox中加载页面,而不是在主窗口中加载 URL 的默认操作。

你需要做两件事:

  1. 将事件处理程序绑定到所有锚标记 ( <a>),以便在单击它们时运行您的事件。
  2. 阻止默认事件处理程序打开 URL (preventDefault)

您可以将事件绑定到所有锚标记,如下所示:

//for all links that exist on this page up to when this line is executed
$("a").click(function(){...});

on()但我建议使用按钮绑定您的事件。这样,所有<a>标签都将运行此事件处理程序,即使它们是您的事件绑定代码运行后创建的。

//for all links that are created on this page even after this lines of code is executed
$("a").on( "click", function(){...});

为了防止默认操作(浏览页面),您只需使用preventDefault()方法:

//assuming event argument is called e
e.preventDefault();

话虽如此,您的代码将如下所示:

<script type="text/javascript">
//this will run when the current document is loaded
$(document).ready(function(){
    $("a").on("click",function(e){
        e.preventDefault();
        //$(this).attr("href") contains the href attribute of the <a> tag
        $.colorbox({opacity:0.3, href:$(this).attr("href")});
    });
});
</script>
于 2012-11-15T14:57:08.147 回答