twitter-bootstrap Twitter 引导程序 - 通过单击链接显示模态

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

Twitter bootstrap - show modal by clicking on a link

javascriptrubytwitter-bootstrapsinatra

提问by Alexandre

I'm using twitter bootstrap. I want modal to be shown by clicking on a link using javascript.

我正在使用推特引导程序。我希望通过使用 javascript单击链接来显示模态。

$("#my-link-id").click($(this).modal());

However, the code above causes an error of

但是,上面的代码会导致错误

Uncaught Error: HIERARCHY_REQUEST_ERR: DOM Exception 3 

What did I do wrong?

我做错了什么?

回答by andlrc

You are calling the method modalbefore the user click's #my-link-id, the fix:

modal在用户单击之前调用该方法#my-link-id,修复:

$("#my-link-id").click(function() {
    $(this).modal();
});

or:

或者:

$("#my-link-id").on("click", function() {
    $(this).modal();
});


To see an explanation on the error you can read here: What exactly can cause an "HIERARCHY_REQUEST_ERR: DOM Exception 3"-Error?

要查看有关错误的解释,您可以在此处阅读:究竟什么会导致“HIERARCHY_REQUEST_ERR: DOM Exception 3”-错误?

回答by iaq

need to do preventDefault on the event to stop the click following the link instead of opening the modal.

需要对事件执行 preventDefault 以停止点击链接而不是打开模态。

    $("#my-link-id").click(function(e) {
        e.preventDefault();
        $(this).modal();
    });