【问题标题】:Remove element from HTML with jQuery使用 jQuery 从 HTML 中删除元素
【发布时间】:2011-01-04 09:28:41
【问题描述】:

我正在构建一个系统,用户可以在其中构建自己的导航系统,该过程是当用户访问该站点时,他们可以选择一个可用主题列表,然后选择一个顶部创建和包含所有主题内容的手风琴,点击再次主题应该删除顶部,目前我有这个代码,但它不起作用,谁能指导我,

JavaScript

$("a.navlink").click(function(ev) {
        var url = $(this).attr("href")
        var id = $(this).attr("id")
        ev.preventDefault();
        if(!$(this).hasClass('saved')) {
            //$("a.navlink").addClass('active')
                $.ajax ({
                    url: url,
                    type: "POST",
                    data: "method=add&id="+id,
                    success: function (html) {
                        $('#accordion').accordion('destroy');
                        $("#accordion").append(html);
                        $('#accordion').accordion({
                            //active: 0,
                            header:'h2.'+id,
                            collapsible:true
                        });
                    $("a.navlink").addClass('saved');
                    }
                });
        } else if($("a.navlink").hasClass('saved')) {
            $.ajax ({
                url: url,
                type: "POST",
                data: "method=delete",
                success: function (html) {
                    $("a.navlink").removeClass('saved');
                    //$("."+id).remove();
                }
            });    
        }
    });

构建手风琴的 HTML/PHP

    <?php
var_dump($_POST);
if(isset($content)) {
    foreach($category_name as $k => $v) {
        echo "<h2 class=".$this->input->post('id')."><a href='#'>$v[category_name]</a></h2>";
        echo "<div class='$v[category_name]'>";
    }
    $replace = array(".", "png", "gif", "jpg");
    $count = 0;
    foreach($content as $k=>$v) {
    $count ++;
    $image_name = str_replace($replace, "", $v['image_name']);
    echo "<a class='contentlink' href='index.php/home/get_content_abstract/$v[content_id]'>";
    echo "<img src='/media/uploads/".strtolower($v['category_name'])."/".$image_name."_thumb.png' alt='This is the picture' />";
    echo "</a>";
    }
    echo "</div>";
//die(var_dump($content));
}

if(isset($favourites_category)) {
    //die(var_dump($favourites));
    echo "<h2 class=".$this->input->post('id')."><a href='#'>$favourites_category</a></h2>";
    $count = 0;
    $replace = array(".", "png", "gif", "jpg");
    foreach ($favourites as $row) {
        $count ++;
        $image_name = str_replace($replace, "", $row['image_name']);
        echo "<div class='$favourites_category'>";
        echo "<a class='contentlink' href='index.php/home/get_content_abstract/$row[content_id]'>";
        echo "<img src='/media/uploads/".strtolower($row['category_name'])."/".$image_name."_thumb.png' alt='This is the picture' />";
        echo "<a/>";
        echo "</div>";
    }
}
?>

基本上,我需要识别每个已创建的手风琴,如果在屏幕上有手风琴时按下其链接,则手风琴将从屏幕上的 HTML 中删除。

【问题讨论】:

    标签: php javascript jquery ajax codeigniter


    【解决方案1】:

    回调函数的上下文与 ajax 调用不同。您的变量 idurl 在您的回调中无法访问。您可以让它们通过 ajax 调用传递以在回调中使用它,但响应应该是 JSON 而不是 HTML。

    此外,在一些地方(例如 else if),您使用的是 $("a.navlink")(将评估为第一个 a.navlink)而不是 $(this)

    这里有一些更新的代码,但我不太清楚你要做什么

    $("a.navlink").click(function(ev) {
      var url = $(this).attr("href")
      var id = $(this).attr("id")
      ev.preventDefault();
      if(!$(this).hasClass('saved')) {
        //$("a.navlink").addClass('active')
        $.ajax ({
          url: url,
          type: "POST",
          data: {method: 'add', id: id},
          dataType: "json",
          success: function (response) {
            //vars url and id are not accessible here
            //so it needs to be returned from the ajax call
            $('#accordion').accordion('destroy');
            $("#accordion").append(response.html);
            $('#accordion').accordion({
              //active: 0,
              header:'h2',
              collapsible:true
            });
            $("#" + response.id).addClass('saved');
          }
        });
      } else if($(this).hasClass('saved')) {
        $.ajax ({
          url: url,
          type: "POST",
          data: {method: 'delete', id: id},
          dataType: "json",
          success: function (response) {
            $("#" + response.id).removeClass('saved');
            $("h2." + response.id).remove();
          }
        });    
      }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-13
      • 1970-01-01
      • 2015-12-15
      • 1970-01-01
      • 2019-05-09
      • 1970-01-01
      • 2018-01-17
      • 2019-06-27
      相关资源
      最近更新 更多