javascript jQuery - 单击时动态删除头部元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3611895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 01:43:32  来源:igfitidea点击:

jQuery - dynamically remove head elements on click

javascriptjquery

提问by ClosDesign

Does anyone know if you can remove head elements on a button click and how to do it with jQuery?

有谁知道您是否可以在单击按钮时删除头部元素以及如何使用 jQuery 进行删除?

I am trying to get rid of certain script tags from the html head when a button is clicked. For instance. I have 1 screen view with a slideshow controlled by an external javascript file. When I click on a button "Click to get rid of this elements JS" I want to remove the external javascript path from the HTML Head.

单击按钮时,我试图从 html 头中删除某些脚本标记。例如。我有 1 个屏幕视图,带有由外部 javascript 文件控制的幻灯片。当我单击按钮“单击以摆脱此元素 JS”时,我想从 HTML Head 中删除外部 javascript 路径。

Any ideas. Have been at this thing for a week or so.

有任何想法吗。已经在这件事上一个星期左右了。

回答by HandiworkNYC.com

You can add an id to a script element then remove that ID:

您可以向脚本元素添加 id,然后删除该 ID:

<script type="text/javascript" src="init.js" id="initJs" ></script>

<span id="removeScript"></span>

$('#removeScript').click(function() {
     $('#initJs').remove();
});

回答by ClosDesign

I don't think its a good Idea to remove Entire HEADElement. 'Cause your Page may contain some more Elements (i.e., title, style..) which are appendedto HeadElement. If you want to remove a particular scriptElement do something like

我认为删除整个HEAD元素不是一个好主意。因为您的页面可能包含更多附加HeadElement 的元素(即标题、样式...)。如果要删除特定script元素,请执行以下操作

$(function() {
  $('input[type=button]').click(function() {
      $('script[src=path/file.js]').remove();
  });
});

Edit:

编辑

var flag = false;
function breakTheCode() {
    if(!flag) {
        //run your code
    }else return;
 }

$(function() {
  $('input[type=button]').click(function() {
      flag = true; //flag is set, so we no more using/ running your code
      breakTheCode(); //call you function/method
  });
});

回答by treeface

You can do this sort of thing using javascript, sure, but before you do it, you might want to ask yourself again why. Here's a link describing how to do it in pure javascript with a jquery example provided by the other answerer:

当然,您可以使用 javascript 执行此类操作,但在执行此操作之前,您可能需要再问自己为什么。这是一个链接,描述了如何使用其他回答者提供的 jquery 示例在纯 javascript 中执行此操作:

http://www.javascriptkit.com/javatutors/loadjavascriptcss2.shtml

http://www.javascriptkit.com/javatutors/loadjavascriptcss2.shtml

But try to keep in mind that most modern browsers will keep these external resources in memory for at least as long as the page is open. Therefore, you won't really be doing much.

但请记住,大多数现代浏览器都会将这些外部资源保存在内存中,至少只要页面打开。因此,您实际上不会做太多事情。

回答by Fabien

For my part the best solution is to use ID on the scripts. I read many page over the web, try many solutions, and the only one who works fine every time is to remove a script like a div with an id !

就我而言,最好的解决方案是在脚本上使用 ID。我在网上阅读了很多页面,尝试了很多解决方案,唯一每次都能正常工作的解决方案是删除带有 id 的 div 之类的脚本!

回答by Anup

For remove js file : $("script[src='your.js']").remove();

对于删除 js 文件: $("script[src='your.js']").remove();