排除从 jQuery 外部文件附加的外部样式表

Exclude an external stylesheet appended from a jQuery external file

我正在为客户建立一个网站,在某个时候我不得不添加第三方 jQuery 文件以便将一些产品添加到我的页面。问题是这个脚本以这种方式将他自己的样式表添加到我的文件中:

jQuery.ajaxSetup({
    async: false
});

jQuery('head').append('<link type="text/css" rel="stylesheet" href="http://pathToTheScript/style-products-v2.css">');

我想知道是否有办法从脚本中排除此功能。我可以在本地下载,也可以手动删除文件,但问题是这个第三方会定期更新此页面,添加大量新产品。 我在 wordpress 环境中工作,所以我的问题是:有没有办法用另一个脚本排除该文件,以便我可以用我的旧样式表替换它?

移除

jQuery('head').append('<link type="text/css" rel="stylesheet" href="pathToTheScript/style-products-v2.css">');

来自脚本。

试试这个:

$(document).ready(function(){
    $("link").each(function(){
        if($(this).attr("href")=="pathToTheScript/style-products-v2.css"){
            $(this).remove();
        }
    });
});




编辑
因为这个link是被另一个脚本插入的...
.css 文件的文件名可能会在未来的第三方脚本更新中改变...

您应该通过使用正则表达式来定位元素来执行此操作:

$(document).ready(function(){
    $("link").each(function(){
        if($(this).attr("href")==/http:\/\/domain\.com\/pathToTheScript\/.*\.css/){
            $(this).remove();
        }
    });
});

这将删除具有如下 href 值的 link 元素:http://domain.com/pathToTheScript/ (whatever) .css:

示例:
http://domain.com/pathToTheScript/style-products-v2.css
http://domain.com/pathToTheScript/style-products-v3.css
http://domain.com/pathToTheScript/improved-style.css
http://domain.com/pathToTheScript/anything.css

旁注:
在你保留在正则表达式中的路径部分(你确定不会改变的部分),你必须转义 /. 像这样 \/ 和这样: \..
你可以在这里测试你的正则表达式:https://regex101.com/#javascript

说清楚
您必须使用 CSS 文件的路径 !
不是注入它的 .js 文件的路径。
;)

没有测试,但像这样的东西应该可以工作:

$(document).ready(function() {
    $('link[href="pathToTheScript/style-products-v2.css"]').remove();
});