php - 单击链接调用原型(prototype)(javascript)函数?

标签 php javascript prototypejs

我正在使用带有原型(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/

相关文章:

php - 高效的mysql数据库结构建议

javascript - 如何在切换仪表板过滤器后更新 Tableau 扩展 API 图表?

javascript 字符串 : remove all except numbers and the first "."

javascript - 异步AJAX按顺序调用

PHP 转义存储过程参数

php - 如何使用 "every first thursday in month"获取 DatePeriod?

javascript - 更新 : How to find event listeners on a DOM node in prototype?

jquery - jquery 中是否有原型(prototype)序列化()的等效项

php - 根据浏览器更改下载地址

javascript - 如何在iframe元素中更改样式属性