避免为每个元素重复javascript

时间:2015-03-11 14:56:07

标签: javascript performance dialog modal-dialog polyfills

我在我的网站上使用Google dialog-polyfill作为模式。我需要在一个可以独立启动的页面上有多个模态。现在,我可以开始工作的唯一方法是重复所有的javascript,这看起来效率很低:

<dialog id="dialog" name="modal">
    <p>This is the login modal dialog.</p>
    <input type="text"></input>
    <p><button id="close-dialog">Close dialog</button></p>
</dialog>
<p><button id="open-dialog">Open login dialog</button></p>

<dialog id="dialog2" name="modal">
    <p>This is the registration modal dialog.</p>
    <input type="text"></input>
    <p><button id="close-dialog2">Close dialog</button></p>
</dialog>
<p><button id="open-dialog2">Open registration dialog</button></p>

<script>
    var dialog = document.getElementById('dialog');
    var dialog2 = document.getElementById('dialog2');
    dialogPolyfill.registerDialog(dialog);
    dialogPolyfill.registerDialog(dialog2);

    document.getElementById('open-dialog').addEventListener('click', function () {
        dialog.showModal();     });
    document.getElementById('close-dialog').addEventListener('click', function () {
        dialog.close();     });

    document.getElementById('open-dialog2').addEventListener('click', function () {
        dialog2.showModal();     });
    document.getElementById('close-dialog2').addEventListener('click', function () {
        dialog2.close();     });

    dialog.addEventListener('click', function (event) {
        var rect = dialog.getBoundingClientRect();
        var isInDialog = (rect.top <= event.clientY && event.clientY <= rect.top + rect.height && rect.left <= event.clientX && event.clientX <= rect.left + rect.width);
        if (!isInDialog) {
            dialog.close();
        }
    });

    dialog2.addEventListener('click', function (event) {
        var rect = dialog2.getBoundingClientRect();
        var isInDialog = (rect.top <= event.clientY && event.clientY <= rect.top + rect.height && rect.left <= event.clientX && event.clientX <= rect.left + rect.width);
        if (!isInDialog) {
            dialog2.close();
        }
    });
</script>

我玩过这个并添加name属性无济于事:

    var names = document.getElementsByName("modal");
    for (var i = 0; i < names.length; i++) {
        names[i].onchange = function () {
            document.getElementById('open-dialog').addEventListener('click', function () {
                dialog.showModal();
            });
            document.getElementById('close-dialog').addEventListener('click', function () {
                dialog.close();
            });
        }
    }

我没有看到课程如何解决这个问题。有没有办法压缩代码,以便我不需要为每个模态对话框重复它?

2 个答案:

答案 0 :(得分:1)

我编辑了你的小提琴:

http://jsfiddle.net/178f81qc/2/

这是你的意思吗?

HTML

<div class="dialog-container">
    <dialog>This is dialog one <button class="close">close</button></dialog>

    <button class="open">open dialog one</button>
</div>

<br>

<div class="dialog-container">
    <dialog>This is dialog two <button class="close">close</button></dialog>

    <button class="open">open dialog two</button>
</div>

的Javascript

var dialogContainers = document.querySelectorAll('.dialog-container'), i;
for (i=0; i<dialogContainers.length; i++) {

    var dialog = dialogContainers[i].querySelector('dialog'), // the <dialog> element
        btnOpen = dialogContainers[i].querySelector('.open'),  // the open button
        btnClose = dialogContainers[i].querySelector('.close'); // the close button

    // for old browsers
    dialogPolyfill.registerDialog(dialogContainers[i]);

    btnOpen.addEventListener('click', function () {
        this.showModal();
    }.bind(dialog));

    // because you bind 'dialog' to the function, 'this' will be the dialog.
    // (if you don't use bind, 'this' will be btnOpen instead)

    btnClose.addEventListener('click', function () {
        this.close();
    }.bind(dialog));
}

答案 1 :(得分:0)

我明白了。我在对话框中添加了一个类,以便我可以获得所有这些;我可能只是通过标签名称选择,但这会锁定我只进入模态对话框。然后我修改了我的脚本:

<script>
    var els = document.getElementsByClassName("modal");
    Array.prototype.forEach.call(els, function(el) {

        var dialogid = el.id;
        var dialog = document.getElementById(dialogid);
        dialogPolyfill.registerDialog(dialog);

        document.getElementById('open-'+dialogid).addEventListener('click', function () {
            dialog.showModal();
        });
        document.getElementById('close-'+dialogid).addEventListener('click', function () {
            dialog.close();
        });
        dialog.addEventListener('click', function (event) {
            var rect = dialog.getBoundingClientRect();
            var isInDialog = (rect.top <= event.clientY && event.clientY <= rect.top + rect.height && rect.left <= event.clientX && event.clientX <= rect.left + rect.width);
            if (!isInDialog) {
                dialog.close();
            }
        });
    });
</script>

我真的不喜欢回答我自己的问题,但这很有效。如果有人能提出更有效的方法,我很乐意给予他信任。