将多个点击压缩为一个

Condensing multiple onclicks into one

这是我当前的代码:

document.getElementsByName('links')[0].onclick = function (event) {
event = event || window.event;
var target = event.target || event.srcElement,
    link = target.src ? target.parentNode : target,
    options = {index: link, event: event},
    links = this.getElementsByTagName('a');
blueimp.Gallery(links, options);
};

document.getElementsByName('links')[1].onclick = function (event) {
event = event || window.event;
var target = event.target || event.srcElement,
    link = target.src ? target.parentNode : target,
    options = {index: link, event: event},
    links = this.getElementsByTagName('a');
blueimp.Gallery(links, options);
};

我意识到这种复制和粘贴的效率非常低(而且当我需要添加 [2].onclick[3].onclick 等时只会变得更糟),所以有没有一种方法可以压缩他们都在一起吗?

本人初学Javascript,这个问题太简单了还请见谅

jQuery 顺便说一句,你只需要将重复的代码封装起来,例如:

<script>
var elements = document.getElementsByName('links');
Array.prototype.forEach.call(elements, function (el) {
    el.onclick = function (event) {
        event = event || window.event;
        var target = event.target || event.srcElement,
            link = target.src ? target.parentNode : target,
            options = {index: link, event: event},
            links = this.getElementsByTagName('a');
        blueimp.Gallery(links, options);
    };
});
</script>