使用javascript / jquery完全删除具有特定类的div的所有实例?

时间:2014-09-26 14:43:38

标签: javascript jquery html css removeclass

我正在使用Toddish的Popup.js。 http://docs.toddish.co.uk/popup/demos/

长话短说,弹出式插件默认创建div,给定类" .popup_back"和" .popup_cont"。 我有另一个按钮,我希望按下这些按钮,在生成并添加到html后,应该完全删除添加了这些类的div。好像他们从未存在过。当然这有可能吗?

我试过运行一个只运行的函数:

$(".popup_back").remove();
$(".popup_cont").remove();

如下例所示: http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_dom_remove

不幸的是,尽管代码正在运行,但实际的div根本不会被删除。

有什么想法吗?我是这类新手,并且已经搜索过很多关于DOM等的内容,但我还没有解决它。

由于

编辑: 在回复评论时:

Javascript:

function removePopups() { // This function is called to remove the popups.
   console.log("removing...");
   $(".popup_back").remove();
   $(".popup_cont").remove();
}



function func(url) { // url is the url of the image to be displayed within the popup.
   removePopups(); // As soon as the function casillas is called, removePopups is used to remove any existing instances of the divs.

   $('a.theimage').popup({ // This is where the Popup plugin is utilised.
       content : $(url),
       type : 'html'
   });
}

HTML:

<a class="theimage" onclick="func('image/image1.jpg')" href="#" >

长话短说,弹出窗口中会显示一个图像。 我认为问题是弹出插件由于类而运行,但是当发生单击时函数func实际上从未运行。但同时&#34;删除......&#34;仍然在控制台中打印出来,告诉我正在执行的功能。问题是我希望弹出插件与javascript函数一起运行。这个冲突有解决方案吗?

1 个答案:

答案 0 :(得分:0)

您的实现应该如此简单:

<a class="theimage" href="#" >Open</a>

将弹出窗口创建绑定到弹出链接:

$('a.theimage').popup({
    content : 'image/image1.jpg',
    type : 'html'
});

我在这里推测,但可能发生的事情是你通过将popup()调用绑定到标记中的单击处理程序来调用弹出窗口两次。弹出插件已将弹出窗口创建绑定到单击事件。

查看工作demo。注意3外部资源:弹出CSS,弹出JS和jQuery JS。