繁体   English   中英

如何在 javascript 中动态附加事件?

[英]How to attach event dynamically in javascript?

我正在尝试覆盖链接(在 ajax 链接中进行转换)但是当我尝试将事件添加到标签时没有任何反应。

我的剧本。

<script type="text/javascript">
    $j(document).ready(function() {
        $j(".will_paginate_styles a").each(function() {
            if (this.innerText.length > 1) {
                this.style.padding = "8px 6px 10px 6px";
                var url = this.href + "&category_id=<%= 1 %>";
                this.href = "#";
                jQuery(this).live("click",
                     function(){
                                new Ajax.Request(url, {asynchronous:true, evalScripts:true, parameters:'authenticity_token=' + encodeURIComponent('uurmOp9OrgS1CRpcpKdMN9PHlMTt+lkHjhYdNgP+LbQ=')}); return false;
                               }
                );
            }

        });
    });
</script>

如何将事件附加到此标签或将链接转换为 ajax 链接? 有什么建议么?

我正在使用 JQuery。

此行属于原型库

new Ajax.Request(url, {asynchronous:true, evalScripts:true, parameters:'authenticity_token=' + encodeURIComponent('uurmOp9OrgS1CRpcpKdMN9PHlMTt+lkHjhYdNgP+LbQ=')}); return false;

所以我在 jQuery 中将这一行替换为他的等价物:

$.ajax(url: "www.myurl.com", data: { data1: "data1", etc: "etc"})

显然,这种方法有更多的选择。 阅读JQuery官方文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM