Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
删除javascript创建的内容_Javascript_Ajax_Jquery - Fatal编程技术网

删除javascript创建的内容

删除javascript创建的内容,javascript,ajax,jquery,Javascript,Ajax,Jquery,我有一些java脚本在导航菜单后面工作,用户单击导航按钮,一些AJAX激发并引入一些HTML,我想要的是,如果再次单击相同的链接,那么该特定按钮加载的内容将从标记中删除 有人有想法吗?我的代码目前为 $("#Blog").click(function (ev) { ev.preventDefault() var url = $(this).attr("href"); $.ajax ({ url: "in

我有一些java脚本在导航菜单后面工作,用户单击导航按钮,一些AJAX激发并引入一些HTML,我想要的是,如果再次单击相同的链接,那么该特定按钮加载的内容将从标记中删除

有人有想法吗?我的代码目前为

$("#Blog").click(function (ev) {
        ev.preventDefault()
           var url = $(this).attr("href");
            $.ajax ({
                url: "index.php/home/category",
                type: "GET",
                success : function (html) {
            //alert("Success");
                    $("#accordion").append(html);
                }
            });
        });
默认值后缺少逗号


默认值后缺少逗号。

所以我对jQuery的了解还不够,无法用这种方式回答,但为什么不使用一个简单的布尔开关呢

// Pseudo:
If clicked and the switch is false, show the HTML, and set the Switch to True

If clicked and the switch is true, hide the HTML, and set the Switch to False
这应该可以解决问题

下面的代码可能大错特错,但我将用它来解释我的想法:

//Global Variables
var switch = false;

//Function
if(!switch)
{
     $("#Blog").click(function (ev) {
        ev.preventDefault()
           var url = $(this).attr("href");
            $.ajax ({
                url: "index.php/home/category",
                type: "GET",
                success : function (html) {
                //alert("Success");
                    $("#accordion").append(html);
                }
            });
        });

    switch = true;
}
else
{
    $("#accordion").innerHTML = "";
    switch = false;
}

所以我对jQuery的了解还不够,无法用这种方式回答,但为什么不使用一个简单的布尔开关呢

// Pseudo:
If clicked and the switch is false, show the HTML, and set the Switch to True

If clicked and the switch is true, hide the HTML, and set the Switch to False
这应该可以解决问题

下面的代码可能大错特错,但我将用它来解释我的想法:

//Global Variables
var switch = false;

//Function
if(!switch)
{
     $("#Blog").click(function (ev) {
        ev.preventDefault()
           var url = $(this).attr("href");
            $.ajax ({
                url: "index.php/home/category",
                type: "GET",
                success : function (html) {
                //alert("Success");
                    $("#accordion").append(html);
                }
            });
        });

    switch = true;
}
else
{
    $("#accordion").innerHTML = "";
    switch = false;
}
尝试使用而不是。单击:

这将允许您添加第二个函数,在再次单击按钮时删除内容

 $("#Blog").toggle(function (ev) {
    ev.preventDefault();
    var url = $(this).attr("href");
    $.ajax ({
       url: "index.php/home/category",
       type: "GET",
       success : function (html) {
             //alert("Success");
             $("#accordion").append(html);
          }
       });
    },
    function (ev) {
       // remove content from accordion here
    });
尝试使用而不是。单击:

这将允许您添加第二个函数,在再次单击按钮时删除内容

 $("#Blog").toggle(function (ev) {
    ev.preventDefault();
    var url = $(this).attr("href");
    $.ajax ({
       url: "index.php/home/category",
       type: "GET",
       success : function (html) {
             //alert("Success");
             $("#accordion").append(html);
          }
       });
    },
    function (ev) {
       // remove content from accordion here
    });
$(“#手风琴”)。追加(
$(“”)。附加(html)
);
然后您就可以轻松地执行
$('.ajaxcontent').remove()

另一个选项是执行
$(“#accordion:last child”).remove(),但这有点粗糙。

$(“#手风琴”).append( $(“”)。附加(html) );
然后您就可以轻松地执行
$('.ajaxcontent').remove()


另一个选项是执行
$(“#accordion:last child”).remove(),但这有点粗糙。

这是一个分号,它是可选的(虽然使用分号很好),它是可选的(虽然使用分号很好),它是可选的(虽然使用的很好)切换会中断脚本,错误控制台告诉我(ev)未定义示例中没有提到用于切换回调的事件对象。我在一个简单的实验中测试了它,它似乎对我有效<代码>$('h1')。切换(函数(ev){ev.preventDefault();警报(ev)},/*第二个函数*/)切换中断脚本,错误控制台告诉我(ev)未定义示例没有提到切换回调的事件对象。我在一个简单的实验中测试了它,它似乎对我有效<代码>$('h1')。切换(函数(ev){ev.preventDefault();警报(ev)},/*第二个函数*/)