简体   繁体   中英

Twitter bootstrap - show modal by clicking on a link

I'm using twitter bootstrap. I want modal to be shown by clicking on a link using 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?

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

$("#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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM