【问题标题】:JavaScript function to link a CSS stylesheet on click单击时链接 CSS 样式表的 JavaScript 函数
【发布时间】:2014-04-09 08:13:10
【问题描述】:

我想在点击以下链接(样式化为按钮)时链接 CSS 样式表:

<a class="changeView-left" onclick="loadcssfile()" href="#"></a>

这是我的 JavaScript 代码:

function loadcssfile(){
    var fileref=document.createElement("link")
    fileref.setAttribute("rel", "stylesheet")
    fileref.setAttribute("type", "text/css")
    fileref.setAttribute("id", "mobileSheet")
    fileref.setAttribute("href", alternateView.css)
    document.getElementsByTagName("head")[0].appendChild(fileref)
}

这个 JavaScript 函数应该加载样式表,但它没有,我怎样才能让它加载样式表?

【问题讨论】:

  • 好的 - 你的问题是什么?
  • 它不起作用吗?什么是alternateView.css?
  • 提供更多描述,您面临什么错误以及您想要什么结果?
  • 终止符 ; 缺失并在 anchor tag 中添加一些文本以进行检查。
  • fileref.setAttribute("href", "alternateView.css");

标签: javascript hyperlink stylesheet


【解决方案1】:

这个 Javascript 应该加载 css 文件:

function loadcssfile() {
    document.getElementsByTagName("link")[0].setAttribute('href', 'second.css');
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-09
    • 1970-01-01
    • 2017-09-13
    • 2010-11-18
    • 1970-01-01
    • 1970-01-01
    • 2014-07-23
    相关资源
    最近更新 更多