onclick事件监听器无法正常工作?

时间:2014-09-10 06:26:37

标签: javascript javascript-events event-listener

我不太了解(纯)JS,我有一个问题 - 我添加了事件监听器,用于链接类' insideLink',但它不起作用,你能不能解释我为什么?

function MyApp()
{
    this.addEventListeners = function()
    {
        var links = document.getElementsByClassName('insideLink');

        for(var i = 0; i < links.length; i++)
        {
            var element = links[i];
            element.onclick = function(event)
            {
                event = event || window.event
                event.preventDefault ? event.preventDefault() : (event.returnValue=false);

                console.log('Clicked');
            }
        }
    }
}

app = new MyApp();
app.addEventListeners();

1 个答案:

答案 0 :(得分:0)

刚刚从href移动链接到data-href(e.preventDefault()无法正常工作):

<a href="#" data-href="post/view/3" class="insideLink btn btn-info"><span class="glyphicon glyphicon-comment"></span>{{storyCommentsBtn}}</a>