【问题标题】:Changing a css file using jquery doesnt work使用 jquery 更改 css 文件不起作用
【发布时间】:2023-03-27 06:38:01
【问题描述】:

我已经搜索了堆栈溢出和网络,但我找不到答案,至少我不明白。以我对这个主题的有限知识(jquery 和 java)。所以基本上我想在某些功能发生后更改其中一个 css 类(#navbar a)。

我尝试在脚本中实现这一点,但没有成功:

$('link[href="Style.css"]').addClass('#navbar a').css('width', '20px');

这是javascript/jQuery:

<script>
window.onscroll = function() {myFunction()};

var navbar = document.getElementById("navbar");
var sticky = navbar.offsetTop;

function myFunction() {
  if (window.pageYOffset >= sticky) {
    navbar.classList.add("sticky")
        navbar.style.align = 'auto';
        navbar.style.width = '100%';
        $('link[href="Style.css"]').addClass('#navbar a').css('width', '20px');
  } else {
    navbar.classList.remove("sticky");
      navbar.style.width = '80%';
  }
}
</script>

这是css文件/样式文件:

#navbar a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  width: 25%;
  text-decoration: none;
  font-size: 35px;
  padding: 20px 0px;
}

这是整个html代码:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width;initial-scale=1;height=device">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="Style.css">
</head>
<body style="background:#101010">
<div class="header">
  <h2>FELLE</h2>
</div>
<div id="navbar">
  <a class="active" href="">Hjem</a>
  <a href="Historie.html">Historie</a>
  <a href="Hyttefelt.html">Hyttefelt</a>
  <a href="Annet.html">Annet</a>
</div>
<div class="container" align="middle">
    <img src="Bilder/Felle_Butikk.jpg" alt="Felle butikk"/>
    <object data="Tekster/Felle_Butikk.txt"></object>
    <img src="Bilder/Solhomfjell.jpg" alt="Solhomfjell i nissedal"/>
    <object data="Tekster/Solhomfjell.txt"></object>
</div>

<script>
window.onscroll = function() {myFunction()};

var navbar = document.getElementById("navbar");
var sticky = navbar.offsetTop;

function myFunction() {
  if (window.pageYOffset >= sticky) {
    navbar.classList.add("sticky")
        navbar.style.align = 'auto';
        navbar.style.width = '100%';
        $('link[href="Style.css"]').addClass('#navbar a').css('width', '20px');
  } else {
    navbar.classList.remove("sticky");
      navbar.style.width = '80%';
  }
}
</script>
</body>
</html>

【问题讨论】:

  • 请同时添加 html 代码。谢谢
  • 如果你想改变元素的css然后使用$('#navbar a').css('width', '20px')
  • 我觉得自己很愚蠢。这就像@Carsten Løvbo Andersen 的魅力,我的错。生病删除这篇文章,看到它也被标记为重复。对不起,我的英语不是我的母语

标签: jquery html css


【解决方案1】:

$('link[href="Style.css"]').addClass('#navbar a').css('width', '20px');
<link href="Style.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

您有上面的代码,但这不会修改样式表。它修改了链接元素。

由于元素一开始是不可见的,给它一个宽度没有任何效果。


您可以修改实际元素的style 属性(并完全包含样式表):

jQuery('#navbar a').css('width', '20px');

或者您可以see this question 自行更改 CSS 规则。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多