通过JavaScript打开UIKit模式

问题描述 投票:1回答:1

我正在使用UIKit框架,他们的模态看起来像这样:

<div id="modal-id" uk-modal>
    <div class="uk-modal-dialog">
        <button class="uk-modal-close-default" type="button" uk-close></button>
        <div class="uk-modal-header">
            <h2 class="uk-modal-title">Modal Title</h2>
        </div>
        <div class="uk-modal-body">
            <p>Lorem ipsum dolor sit amet</p>
        </div>
        <div class="uk-modal-footer uk-text-right">

            <button class="uk-button uk-button-primary" type="button">Ok</button>
        </div>
    </div>
</div>

它通常用一个按钮打开:

<a id='open-modal' uk-toggle="target: #modal-id" href="#">Open</a>

我想将它链接到按键,所以当按下H时它会切换模态:

document.body.onkeyup = function(e){
    if(e.keyCode === 72){
        console.log('H key pressed');
         // Toggle Modal
    }
}

我使用.stigger('click')在模态按钮id上使用Bootstrap,但是它不能用于UIKIT

javascript modal-dialog getuikit
1个回答
1
投票

所以基本上你需要用Javascript触发模态。这在documentation中有描述

document.body.onkeyup = function(e){
    if(e.keyCode === 72){
        console.log('H key pressed');
        // Toggle Modal
        UIkit.modal('#modal-id').toggle(); //you can use toggle or open here
    }
}
<!-- UIkit CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/uikit/3.0.3/css/uikit.min.css" />

<!-- UIkit JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/uikit/3.0.3/js/uikit.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/uikit/3.0.3/js/uikit-icons.min.js"></script>

<div id="modal-id" uk-modal>
    <div class="uk-modal-dialog">
        <button class="uk-modal-close-default" type="button" uk-close></button>
        <div class="uk-modal-header">
            <h2 class="uk-modal-title">Modal Title</h2>
        </div>
        <div class="uk-modal-body">
            <p>Lorem ipsum dolor sit amet</p>
        </div>
        <div class="uk-modal-footer uk-text-right">

            <button class="uk-button uk-button-primary" type="button">Ok</button>
        </div>
    </div>
</div>

<a id='open-modal' uk-toggle="target: #modal-id" href="#" class="uk-button uk-button-primary">Open</a>
© www.soinside.com 2019 - 2024. All rights reserved.