简体   繁体   English

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

[英]Completely delete all instances of a div with a specific class using javascript/jquery?

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

Long story short, the popup plugin creates divs by default given the classes ".popup_back" and ".popup_cont". 长话短说,对于给定的类“ .popup_back”和“ .popup_cont”,默认情况下,popup插件会创建div。 I have another button I wish to press which should completely delete the added divs with those classes after they have been generated and added to the html. 我要按下另一个按钮,当这些类生成并添加到html之后,它们应该完全删除带有这些类的已添加div。 As if they never even existed. 好像它们甚至不存在。 Surely this is possible? 当然可以吗?

I have tried running a function which simply runs: 我试过运行一个简单运行的函数:

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

As shown in this example: http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_dom_remove 如以下示例所示: http : //www.w3schools.com/jquery/tryit.asp?filename=tryjquery_dom_remove

Unfortunately despite the code running, the actual divs are never deleted as required. 不幸的是,尽管代码正在运行,但实际的div从未按需删除。

Any ideas? 有任何想法吗? I am new to this kind of thing and have googled around and read a lot about DOM etc but am yet to crack it. 我对这种事情是陌生的,已经在google上搜索了很多关于DOM等的文章,但是还没有破解。

Thanks 谢谢

EDIT: In reply to the comments: 编辑:回复评论:

The Javascript: 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'
   });
}

The HTML: HTML:

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

Long story short, an image is displayed in the popup. 长话短说,图像显示在弹出窗口中。 I think the issue is that the popup plugin runs due to the class but the function func is never actually run when the click occurs. 我认为问题在于弹出式插件由于该类而运行,但是当单击发生时,函数func实际上从未运行。 However simultaneously "removing..." still prints out in the console which tells me that the function IS being executed. 但是,同时在控制台中仍然打印出“ removing ...”,告诉我该功能正在执行中。 The problem is I want the popup plugin to run together with the javascript function. 问题是我希望弹出插件与javascript函数一起运行。 Is there a solution for this conflict? 有解决此冲突的方法吗?

Your implementation should really be as simple as this: 您的实现实际上应该像这样简单:

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

Bind the popup creation to your popup link: 将弹出窗口创建绑定到您的弹出链接:

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

I'm speculating here, but what might be happening is that you're invoking the popup twice by binding the popup() call to a click handler in your markup. 我在这里推测,但是可能发生的情况是,您通过将popup()调用绑定到标记中的单击处理程序来两次调用弹出窗口。 The popup plugin already binds the popup creation to a click event. 弹出插件已经将弹出创建绑定到click事件。

View working demo . 查看工作演示 Note the 3 external resource: the popup CSS, the popup JS, and the jQuery JS. 注意3个外部资源:弹出CSS,弹出JS和jQuery JS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM