如何为所有 AJAX 链接编写一个 jquery 函数 [英] How to code one jquery function for all AJAX links

查看:25
本文介绍了如何为所有 AJAX 链接编写一个 jquery 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 windows 上使用 zend 框架.我想第一次在我的项目中实现ajax.我寻求帮助并创建了一个非常简单的 ajax 功能.

I am using zend framework on windows. I want to implement ajax in my project first time. I searched for help and created a very simple ajax functionality.

IndexController.php

public function indexAction() {
}

public function oneAction() {
}

public function twoAction() {
}

index.phtml

<script type="text/javascript" src="js/jquery-1.4.2.js"></script>
<script type="text/javascript" src="js/AJAX.js"></script>

<a href='http://practice.dev/index/one' class='one'>One</a>
<a href='http://practice.dev/index/two' class='two'>Two</a>
<br /><br />
<div id="one">one.phtml content comes here</div>
<div id="two">two.phtml content comes here</div>

AJAX.js

jQuery(document).ready(function(){
    jQuery('.one').click(loadOne);
    jQuery('.two').click(loadTwo);
});

function loadOne(event) {

    event.preventDefault();

    jQuery.ajax({
        url: '/index/one',
        success: function( data ) {
                    jQuery('#one').html(data);
                    }
    });
}

function loadTwo(event) {

    event.preventDefault();

    jQuery.ajax({
        url: '/index/two',
        success: function( data ){
                    jQuery('#two').html(data);
                    }
    });
}

当点击链接时,上面的代码正在工作并分别在一个"和两个"DIV中加载one.phtml和two.phtml的数据.您可以看到我必须为每个链接创建单独的 jquery 函数,并且还必须为每个链接标签添加新类.

Above code is working and loading data of one.phtml and two.phtml in 'one' and 'two' DIVs respectively when its link is clicked. You can see that I have to create separate jquery function for each link and also have to add new class for each link tag.

我想做什么?:

我只想对所有 AJAX 请求使用一个 jquery 函数,并且不想在该函数中硬编码 urlsuccess 属性.

I want to use only one jquery function for all AJAX requests and don't want to hard code url and success attributes in that function.

当我将AJAX"类添加到任何链接标签时,它应该使用 AJAX 加载内容.

When I add "AJAX" class to any link tag then it should load content using AJAX.

谢谢.

推荐答案

为了在 div 中简单加载数据,我会使用 load 方法

for simple loading of data in divs i would use the load method

HTML

<script type="text/javascript" src="js/jquery-1.4.2.js"></script>
<script type="text/javascript" src="js/AJAX.js"></script>

<a href='http://practice.dev/index/one' class='ajax' rel="one">One</a>
<a href='http://practice.dev/index/two' class='ajax' rel="two">Two</a>
<br /><br />
<div id="one">one.phtml content comes here</div>
<div id="two">two.phtml content comes here</div>

JS

jQuery(document).ready(function(){
    jQuery('.ajax').click(function(event){
       event.preventDefault();

       var target = '#' + jQuery(this).attr('rel');
       var href = jQuery(this).attr('href');
       jQuery( target ).load( href );

      });
});

使用单个类来标识所有应该使用 ajax 调用而不是正常使用的链接.并向那些将包含容器 div 的 id 的链接添加一个 rel 属性..

Use a single class to identify all links that should use ajax calls instead of their normal use. And add a rel attribute to those links that will contain the id of the container div..

这篇关于如何为所有 AJAX 链接编写一个 jquery 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆