How to delete HTML Elements in an iFrame using JavaScript

。_饼干妹妹 提交于 2019-12-18 07:20:57

问题


I have an iFrame and a div with content in it. I want to delete the div via JavaScript, is that possible and how could I do that?

I don't want to just not display it (eg. display: none via CSS) but remove it from the HTML of the site. I have basic knowledge of JavaScript but don't have any experience working with an iFrame.


回答1:


You can use

$("#iFrameId").contents().find("#yourDiv").empty();

It is better to use remove()

example: $("#iFrameId").contents().find("#yourDiv").remove();

Explaination

empty() will remove all the contents of the selection.

remove() will remove the selection and its contents and all the event handlers associated with it.

For reference: 1) http://api.jquery.com/remove/ 2) http://api.jquery.com/empty/




回答2:


You can try something like:-

frame.removeChild(//pass div id here);


来源:https://stackoverflow.com/questions/31808185/how-to-delete-html-elements-in-an-iframe-using-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!