用一个ajax调用改变两个div

时间:2014-11-02 11:58:44

标签: javascript jquery ajax codeigniter rss

我的项目有一个功能,允许用户将某些RSS源设置为收藏夹。

我使用的是codeigniter,我的观点是这样的:

<a href="#" data-toggle="collapse">
   <h5><b> <span class="glyphicon glyphicon-star-empty"></span> Favorites </b></i></h5>
</a>

<ul class="list-unstyled collapse in" id="favorites">
   <?php foreach( $source as $fav ) :  if( $fav->favourite == 1 ) {?>
      <li>
         <a id="some_id">  <?php echo $fav->rss_title; ?></a>
      </li>
   <?php } endforeach; } ?>
</ul>

   <!-- all favorite links are placed in a left sidebar....in header, i have a div with different opperations... -->
   <div class="col-sm-12">               
      <?php if ( $isFavourite == 0 ) { ?>
          <li><h4><a class="addfav" id='just_id' href="#favorite">Add this Rss Source to Favourites</a></h4></li>
      <?php } else { ?>
          <li><h4><a class="removefav" id='just_id' href="#favorite">Remove this Rss Source from Favourites</a></h4></li>
      <?php } ?>

      // and other options ...
   </div>

这是我使用ajax的脚本:

<script>
$(document).ready(function(){
    $(document).on('click','.addfav',function(){
        var id=this.id;

        $.ajax( {
                    type: "POST",
                    data: {id:id},
                    url: "<?php echo site_url('rssFeedReader/addFavorites'); ?>",
                    success: function(msg) 
                    {
                        $("#favorites").append(msg);
                    }
                })
    });
});
</script>

一切正常,但我无法做某事......在我添加到收藏夹链接后(通过&#39;追加&#39;),我想更改标题中的选项&#39;添加这个Rss来源最喜欢&#39;将此RSS源删除到收藏夹&#39;。我做了这个,但只有当我重新加载页面时才有效...当我用ajax添加链接时我想这样做....

如何在同一个ajax调用中更改包含这些选项的div?我可以在行&#39; $(&#39;#favorites&#39;)之后做些什么。附加(msg); ?

1 个答案:

答案 0 :(得分:1)

如果要更改选项,请执行以下操作:

$('.addfav').addClass('removefav').removeClass('addfav').text('Remove this Rss Source from Favourites');

之后

$("#favorites").append(msg);

线

这将改变

<a class="addfav" id='just_id' href="#favorite">Add this Rss Source to Favourites</a>

<a class="removefav" id='just_id' href="#favorite">Remove this Rss Source from Favourites</a>

之后你可以明显地将侦听器添加到对象中以添加功能。