简体   繁体   English

使用AJAX加载投资组合项目,而无需重新加载页面

[英]Load portfolio items with AJAX without reloading the page

I have a bunch of portfolio items sorted as tabs on this page. 我有一堆投资组合项目在此页面上按选项卡排序。 Link to the site . 链接到该站点 The site is built with Joomla 2.5 and I have a component that takes care of displaying each portfolio item. 该网站是用Joomla 2.5构建的,我有一个组件来显示每个投资组合项目。 What I need to do is to load each respective portfolio item without reloading the page. 我需要做的是加载每个投资组合项目,而无需重新加载页面。 So basically here is the javascript function that has the AJAX call 所以基本上这里是具有AJAX调用的javascript函数

function ajax_portfolio($pid) {
var url = 'index.php?option=com_jms_portfolio&task=item.ajax_load_portfolio&tmpl=component&id=' + $pid;
alert(url);
var x = new Request({
    url: url, 
    method: 'post',
    evalScripts: true,
    evalResponse: true,  
    onSuccess: function(responseText){
        document.getElementById('ja-content-main').innerHTML = responseText;
        aaa();
    }

    }).send();}

The issue in fact is not the AJAX call cause and the click event of tag, there is no problem with this event. 实际上,问题不是AJAX调用原因和代码的click事件,此事件没有问题。 The problem is to fire the javascript function aaaa() after each ajax call. 问题是在每个ajax调用之后触发javascript函数aaaaa()。 Sorry if I was not clear but the problem is to fire the function aaa() after each ajax call, this function creates the slider for each portfolio item. 抱歉,如果我不清楚,但问题是在每个ajax调用后都会触发函数aaa(),此函数将为每个投资组合项目创建一个滑块。

Remove the existing href attribute of the <a> tags that wrap the images. 删除包装图像的<a>标记的现有href属性。 Then, add the a click handler through javascript to each <a> tag after giving them a unqiue id. 然后,在为它们赋予一个非正常ID后,通过javascript将click处理程序添加到每个<a>标记中。 This will then cause the ajax to be called when clicking on the images instead of redirecting to a new page. 然后,这将导致在单击图像时调用ajax,而不是重定向到新页面。

As for calling the aaa function, I assume the issue is scope since you have not posted the method. 至于调用aaa函数,我认为问题是范围,因为您尚未发布该方法。 To give aaa the correct scope, you can pass an extra parameter to ajax_portfolio to acomplish this. 为了给aaa正确的范围,您可以将一个额外的参数传递给ajax_portfolio来完成此操作。

A JQuery example follows. 下面是一个JQuery示例。

<a port_id="56"><img>...</img></a>

$(document).ready(function() {
  $("a").click(function() {
    ajax_portfolio($(this).attr("port_id"), $(this));
  });
});

// Add the $elem parameter here to track which element called this method.    
function ajax_portfolio($pid, $elem) {
  var url = ...;
  var x = new Request({
    url: url, 
    method: 'post',
    evalScripts: true,
    evalResponse: true,  
    onSuccess: function(responseText){
      document.getElementById('ja-content-main').innerHTML = responseText;
      // Pass the element that was clicked on to the aaa function.
      aaa($elem);
  }
}).send();}

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

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