Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用jQuery更改样式表_Jquery_Stylesheet - Fatal编程技术网

使用jQuery更改样式表

使用jQuery更改样式表,jquery,stylesheet,Jquery,Stylesheet,如何在html上的div标记中使用jquery更改样式表?或者更改样式表的jquery代码是什么 在javascript中,我们使用以下代码: <script type="text/javascript"> function changeStyle() { document.getElementById('stylesheet').href = 'design4.css'; } </script> 函数changeStyle(){ document.getEle

如何在html上的div标记中使用jquery更改样式表?或者更改样式表的jquery代码是什么

在javascript中,我们使用以下代码:

<script type="text/javascript">
 function changeStyle() {
 document.getElementById('stylesheet').href = 'design4.css';
 }
</script>

函数changeStyle(){
document.getElementById('stylesheet').href='design4.css';
}

有jQuery方法吗?

要更新完整主题,最好加载一个新的CSS文件。最容易在服务器端完成,但如果坚持动态加载:

// Don't know jQuery, this is regular JS:
var newCss = document.createElement('link');

newCss.rel = 'stylesheet';
newCss.type = 'text/css';
newCss.href = '/path/to/new/cssfile.css';

document.body.appendChild(newCss);
使用jQuery:

$("#styleshhet").attr('href', 'design4.css');

有更好的方法可以满足您的要求,但是如果您真的想使用jquery删除并添加远程样式表,您可以这样做:

$('link[href^=old_css_file.css]').attr('href', '/path_to_new_css/file.css');

阅读有关的详细信息,我使用此代码启用或禁用基于页面的样式表

 $(document).on('pagebeforeshow', function () {
        var URL = $.mobile.path.parseUrl(window.location).toString().toLowerCase();

        if (URL.indexOf("/investment.aspx") > -1 ||
            URL.indexOf("/employees.aspx") > -1) {
            $("link[href^='../../Public/LongLabels.css']").attr("media", "all");
        }
        else {
            $("link[href^='../../Public/LongLabels.css']").attr("media", "not all");
        }
    });

使用技术术语有助于我们大多数人更好地理解您的问题。
 $(document).on('pagebeforeshow', function () {
        var URL = $.mobile.path.parseUrl(window.location).toString().toLowerCase();

        if (URL.indexOf("/investment.aspx") > -1 ||
            URL.indexOf("/employees.aspx") > -1) {
            $("link[href^='../../Public/LongLabels.css']").attr("media", "all");
        }
        else {
            $("link[href^='../../Public/LongLabels.css']").attr("media", "not all");
        }
    });