代码之家  ›  专栏  ›  技术社区  ›  Tom cmoron

php/jquery/ajax,几乎没有初始问题

  •  1
  • Tom cmoron  · 技术社区  · 14 年前

    早上/下午的伙计们。

    写了一些jquery-ajax-shizz,有点卡住了。我已经完成了调用PHP文件的实际过程,它只是试图让页面上的HTML按照我希望的方式进行更改。我想去掉Ajax调用等中使用的ID为的,并将其替换为从PHP文件传递的HTML。代码如下…

    $(".save_places").click(function() {
      $.ajax({
        url: "{/literal}{$sRootPath}{literal}system/ajax/fan_bus.php",
        type: "POST",
        data: ({id : this.getAttribute('id')}),
        dataType: "html",
        success: function(msg){
          $(this).before(msg);
          $(this).empty();
          alert(msg);
        }
      });
      return false;
    });
    

    HTML非常简单;

    <p class="links">
      <a href="#" class="save_places" id="bus_{$businesses.results[bus].id}_{$sMemberDetails.id}"><img src="{$sThemePath}images/save_places.png" alt="Save to My Places" /></a>
      <a href="#"><img src="{$sThemePath}images/send_friend.png" alt="Send to a Friend" /></a>
    </p>
    

    成功函数中的所有内容都是实验性的代码混合,有什么帮助吗?

    一如既往地感谢你。

    2 回复  |  直到 14 年前
        1
  •  3
  •   Nick Craver    14 年前

    我想你想要的是 .replaceWith() , 这样地:

    $(this).replaceWith(msg);
    

    这将取代 <a></a> 随着内容的回归 msg .

    另外,如果您确定元素有ID,您可以这样做:

    data: {id : this.id},
    
        2
  •  1
  •   Delan Azabani    14 年前
    linksPara.replaceChild(newElements, oldAtag);