我是网页开发新手,有一个技术问题。如果我编写一个 JavaScript 函数并将其放在 HTML 文件的头部或正文中,函数中的变量是否可以直接在 CSS 代码中使用来更改内容定位等内容?或者必须从 CSS 文件调用该函数?
感谢您的所有意见!
最佳答案
您可以通过您自己创建的函数将 CSS 应用到 html 元素上。或者请在此处传递一些代码以使其更加清晰。
一般场景可以是:
<html>
<head>
<script>
function useCSS(){
$("#myElement").css("color","red");
}
</script>
</head>
<body>
<p id="myElement">This has to be changed!</p>
</body>
<script>
// function can be written anywhere in the script file or tags but ,has to be called after the element is loaded on page
useCSS();
</script>
关于javascript - 在 HTML 文件的正文或头部创建的 JS 脚本可以直接在 CSS 代码中使用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52062924/