javascript 在 jquery colorbox/lightbox 中打开 url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8933217/
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-10-26 05:02:38  来源:igfitidea点击:

open url in jquery colorbox/lightbox

javascriptjquerylightboxcolorbox

提问by user899893

I have a page that displays restaurant food menu. And when a person clicks on a particular food item I want to open the link in colorbox. Is there a way I can open the links in colorbox without effecting other anchor tags. I cannot modify the existing code(menu is displayed by 3rd party plugin) but I can add additional script to the page(jquery or php). So I was hoping to add ("a").click(), but I wasn't sure how to get the href link for a particular anchor tag. For now all the links I want to open have string "/restaurants/" in common. Any suggestions about how to solve this problem?

我有一个页面显示餐厅的食物菜单。当一个人点击某个特定的食物时,我想在 colorbox 中打开链接。有没有一种方法可以在不影响其他锚标记的情况下打开 colorbox 中的链接。我无法修改现有代码(菜单由 3rd 方插件显示),但我可以向页面添加其他脚本(jquery 或 php)。所以我希望添加 ("a").click(),但我不确定如何获取特定锚标记的 href 链接。现在我想打开的所有链接都有共同的字符串“/restaurants/”。有关如何解决此问题的任何建议?

回答by Tom

<script type="text/javascript">
  $(document).ready(function() {
    $("a", ".rest-menuitem").click(
      function(event) {
        event.preventDefault();
        var elementURL = $(this).attr("href");
        $.colorbox({iframe: true, href: elementURL, innerWidth: 645, innerHeight: 509});
      });
  });
</script>

Change innerWidthand innerHeightto fit your content

更改innerWidthinnerHeight以适合您的内容