我正在使用带有原型(prototype)和 PHP 的 AJAX。它对我有用,但我需要一些小的改变。以下是我的 AJAX 请求运行代码:
JS/原型(prototype):
function ajaxRequest(url) {
new Ajax.Request( url, {
method: 'get',
onSuccess: function( transport ) {
// get json response
var json = transport.responseText.evalJSON( true );
alert(json);
},
onFailure: function() {
alert('Error with AJAX request.');
}
});
return false;
}
HTML:
<a href='javascript:ajaxRequest("/testajax/ajaxresponse");'>Testing AJAX</a>
问题:
现在我想像这样更改我的链接:
<a href='/testajax/ajaxresponse' class='AjaxLink'>Testing AJAX</a>
因此原型(prototype)函数应该捕获class='AjaxLink'
链接的点击事件,然后获取点击链接的href 部分并继续。我怎样才能为这种链接更改我上面的原型(prototype)函数。
谢谢
最佳答案
如果您有 Prototype 1.7,那么 this way可用:
document.on('click', 'a.AjaxLink', ajaxRequest.curry('/testajax/ajaxresponse'));
否则你将不得不依赖旧的Event.observe
:
$$('a.AjaxLink').invoke('observe', 'click',
ajaxRequest.curry('/testajax/ajaxresponse'));
只需重新阅读问题,我发现您想使用 href 属性。 Jan Pfiefer 非常亲密。
document.on('click', 'a.AjaxLink', function(event, element) {
return ajaxRequest(element.href);
});
关于php - 单击链接调用原型(prototype)(javascript)函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7661812/