这是我目前的代码:
Index.html
AddEventListener('click', 'a[data-requestpartial]', function (obj, e)
{
e.preventDefault();
// Error is happening here. this is undefined. obj is undefined.
var url = this.getAttribute('href');
LoadSection('#container', url, null, function (result)
{
window.history.pushState("", "", url);
});
});
On Script.JS
function AddEventListener(eventType, query, func)
{
var objects = Select(query);
for (var i = 0, len = objects.length; i < len; i++)
{
objects[i].addEventListener(eventType, function (e) { func(objects[i], e) });
}
}
基本上,我正在尝试向ALL'a [data-requestpartial]'添加“click”事件监听器。我想阻止锚默认操作,然后我想做我要求的具体操作。
我怎样才能做到这一点?我已经尝试了“this”和“obj”,然而,它们都是未定义的。
编辑:
function Select(query)
{
return query[0] === '#' ? document.getElementById(query.substring(1)) : document.querySelectorAll(query);
}
这是一个JS小提琴: https://jsfiddle.net/q3Lse37y/2/