【问题标题】:How to remove script when button is clicked. [duplicate]单击按钮时如何删除脚本。 [复制]
【发布时间】:2013-04-30 14:07:55
【问题描述】:

当我点击p 标签时,有谁知道如何从我的 html 页面中删除“js/sample.js”文件:

 <!DOCTYPE html>
 <html>
 <head>
<style>
p { color:red; margin:5px; cursor:pointer; }
</style>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="js/sample.js"></script>
</head>
 <body>
<p>First Paragraph</p>
 <p>Second Paragraph</p>
 <p>Yet one more Paragraph</p>

<script>
$("p").click(function () {
$(' ').removeattr('src');

 });
 </script>
</body>
 </html>

【问题讨论】:

标签: javascript jquery


【解决方案1】:

亲爱的朋友试试下面的代码它会为你工作

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script id="script1" src="js/sample.js"></script>

<script>
    $(function () {

        $("p").click(function () {
            $('script1').att('src').remove();
        });
    });
</script>

【讨论】:

    【解决方案2】:

    有趣...你可以在你的脚本标签中添加一个类,然后在你的点击函数中加入这样的东西:

    $('.myClass').remove();
    

    【讨论】:

    • 完全不同的答案,没用。它将删除类而不是 src attr
    • @The-Val 实际上它会删除具有该类名的元素。
    • 哦。抱歉,我以为是removeClass(),我的错误:) 抱歉@skendrick 并感谢您指出@undefined
    【解决方案3】:

    你可以删除脚本标签

    $('script[src="js/sample.js"]').remove();

    但是,一旦脚本执行完毕,简单地删除该标签不会“撤消”脚本。您需要真正销毁您的 sample.js 脚本创建的任何变量/方法。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-01
    相关资源
    最近更新 更多