简体   繁体   English

jQuery-删除标签脚本

[英]JQuery - Remove tag Script

I need to remove two <script> tags from a page dynamically, at the document ready . 我需要在document ready动态地从页面中删除两个<script>标记。 Every tag has its own id; 每个标签都有自己的ID; I've tried to insert in the code: 我试图在代码中插入:

$("#idPrimoScript").remove();
$("#idSecondoScript").remove();

but nothing happens... 但是什么也没发生...

Some ideas? 有什么想法吗? Thanks 谢谢

As I can understand from your question, you want to remove (actually disable) the scripts included in your 2 script tags. 从您的问题中可以理解,您想要删除(实际上是禁用)2个脚本标签中包含的脚本。 a work around I usually used to make in these situations is to introduce a variable which will act as a flag or a passing signal. 在这些情况下,我通常要做的一项工作是引入一个变量,该变量将用作标志或传递信号。 let's say that I will create a global variable which will be the flage I will be using: 假设我将创建一个全局变量,它将是我将要使用的标志:

var enableMySpecialScript = true

as you can see, I initially set the value of this flag to true, and whenever I need to disable the special script inside my page, I set this flag to false, then inside my script file, i always check for the flag's value,so when it's only "true", the script will execute, meaning that when it's false the script will not run, which is exactly what you are asking for. 如您所见,我最初将这个标志的值设置为true,并且每当我需要禁用页面中的特殊脚本时,都会将此标志设置为false,然后在脚本文件中,我总是检查该标志的值,因此,当它仅是“ true”时,脚本将执行,这意味着当它为false时,脚本将不会运行,这正是您要的。

This way you don't have to mess up with removing stuff or altering functions, and from my experience with this solution, it's a clean and debuggable one :) 这样,您就不必为删除内容或更改功能而烦恼,从我对这种解决方案的经验来看,这是一种干净且可调试的解决方案:)

Apparently (having not tried this myself beyond a simple proof script) the way to do this is to create another function with the same name. 显然(除了简单的证明脚本之外,我自己还没有尝试过)执行此操作的方法是创建另一个具有相同名称的函数。

See: 看到:

How to override a function in another javascript file? 如何在另一个JavaScript文件中覆盖函数?

JavaScript: Overriding alert() JavaScript:覆盖alert()

Override jQuery functions 覆盖jQuery函数

Overriding a function in jQuery plugin 覆盖jQuery插件中的函数

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

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