Javascript 我需要在标记中添加变量

Javascript 我需要在标记中添加变量,javascript,jquery,html,css,Javascript,Jquery,Html,Css,假设有此标记: var variabile_colore="#efdcdc"; 我需要在标签中添加一个变量,我想是这样的,但它不起作用: <style>.foot {color: variabile_colore;} </style> 有人能帮我吗?你可以通过在样式标签上创建一个id来使用javascript的DOM对象: document.getelementbyId('styleid').style.color = "#efdcdc"; 相反,您可以通过在样式标

假设有此标记:

var variabile_colore="#efdcdc";
我需要在标签中添加一个变量,我想是这样的,但它不起作用:

<style>.foot {color: variabile_colore;} </style>

有人能帮我吗?

你可以通过在样式标签上创建一个id来使用javascript的DOM对象:

document.getelementbyId('styleid').style.color = "#efdcdc";

相反,您可以通过在样式标记上创建id来使用javascript的DOM对象:

document.getelementbyId('styleid').style.color = "#efdcdc";
可以使用createElement创建动态样式标记并将其附加到DOM中

$'btn'。单击,函数{ var sheet=document.createElement'style'; 变量颜色='红色'; sheet.innerHTML=p{color:+color+}; document.body.appendChildsheet; } p{ 颜色:黑色; } 你好,p标签

改变 可以使用createElement创建动态样式标记并将其附加到DOM中

$'btn'。单击,函数{ var sheet=document.createElement'style'; 变量颜色='红色'; sheet.innerHTML=p{color:+color+}; document.body.appendChildsheet; } p{ 颜色:黑色; } 你好,p标签

更改使用jQuery:-

$('.foot').css('color', variabile_colore);
使用jQuery:-

$('.foot').css('color', variabile_colore);
这有助于您:

<body>
       <div class="foot" id="foot">Sample Text</div>
      <script>
          var variabile_colore="#efdcdc";
          var x = document.getElementById("foot");
          x.style.color = variabile_colore;   
       </script>
</body>
这有助于您:

<body>
       <div class="foot" id="foot">Sample Text</div>
      <script>
          var variabile_colore="#efdcdc";
          var x = document.getElementById("foot");
          x.style.color = variabile_colore;   
       </script>
</body>
我需要在标签里放一个变量,我想是这样的,但是 不起作用:

<style>.foot {color: variabile_colore;} </style>
不知道它到底是什么意思 您的意思是使用js创建一个样式标记并在其中添加声明吗

如果是,那么

var css= '.foot {color: "#efdcdc";}',
    head = document.head || document.getElementsByTagName('head')[0],
    style = document.createElement('style');
    style.type = 'text/css';
    if (style.styleSheet){
       style.styleSheet.cssText = css;
    } else {
      style.appendChild(document.createTextNode(css));
    }
    head.appendChild(style);
如果样式表中已经存在样式

$("#elementId").addClass('foot');
如果您想添加内联css

$('.foot').css('color', variabile_colore);
我需要在标签里放一个变量,我想是这样的,但是 不起作用:

<style>.foot {color: variabile_colore;} </style>
不知道它到底是什么意思 您的意思是使用js创建一个样式标记并在其中添加声明吗

如果是,那么

var css= '.foot {color: "#efdcdc";}',
    head = document.head || document.getElementsByTagName('head')[0],
    style = document.createElement('style');
    style.type = 'text/css';
    if (style.styleSheet){
       style.styleSheet.cssText = css;
    } else {
      style.appendChild(document.createTextNode(css));
    }
    head.appendChild(style);
如果样式表中已经存在样式

$("#elementId").addClass('foot');
如果您想添加内联css

$('.foot').css('color', variabile_colore);

我认为你需要强制地应用颜色属性

请尝试以下内容

var variabile_colore = "#efdcdc !important";
$('.foot').css('color', variabile_colore);

我认为你需要强制地应用颜色属性

请尝试以下内容

var variabile_colore = "#efdcdc !important";
$('.foot').css('color', variabile_colore);

因为对我来说,它看起来像是你试图从JavaScript生成一个css类。 看看这个

以下是未测试的对您案例的适应性:

var variabile_colore="#efdcdc";

$("<style type='text/css'> .foot{ color:" +  variabile_colore + 
               "; } </style>" ).appendTo("head");

因为对我来说,它看起来像是你试图从JavaScript生成一个css类。 看看这个

以下是未测试的对您案例的适应性:

var variabile_colore="#efdcdc";

$("<style type='text/css'> .foot{ color:" +  variabile_colore + 
               "; } </style>" ).appendTo("head");

使用jquery将css应用于任何元素,而不是类似的东西,看看css的可能副本是否还不完全支持这样的变量;您可能需要研究类似于SASS的东西来实现您想要的功能。使用jquery将css应用于任何元素,而不是这些东西。查看css的可能副本是否还不完全支持这样的变量;你可能想研究一些类似SASS的东西来实现你想要的。