Jquery和十六进制颜色?

时间:2015-05-29 15:38:02

标签: jquery css

如果从html文本中检索十六进制代码颜色,则无法设置颜色。如果我手动设置颜色,它可以正常工作。

HTML

<div class="main-wrapper">
   <div class="html-element-color">#ccff00</div>
   <div class="wrapper">hello</div>
</div>
<div class="main-wrapper">
   <div class="html-element-color">#cccccc</div>
   <div class="wrapper">hello</div>
</div>
<div class="main-wrapper">
   <div class="html-element-color">#f1f1f1</div>
   <div class="wrapper">hello</div>
</div>

此代码可以正常使用

$( ".html-element-color" ).each(function( index ) {

  var bgColorWrapper = "#ccff00";

   $(this).parent().find("div.wrapper").css( "background-color", bgColorWrapper ) ;

})

这里的代码不起作用:

$( ".html-element-color" ).each(function( index ) {

  var bgColorWrapper = $('.html-element-color').text();

   $(this).parent().find("div.wrapper").css( "background-color", bgColorWrapper ) ;

});

1 个答案:

答案 0 :(得分:4)

var line = example.$line[0], $text = example.$text, top = parseFloat(line.style.top).toFixed(1) / 1, lineHeight = parseFloat($text.css('line-height')).toFixed(1) / 1; // UP KEY PRESSED if (move == 'up') { line.style.top = (top - lineHeight) + 'px'; return; } // DOWN KEY PRESSED if (move == 'down') { line.style.top = (top + lineHeight) + 'px'; } 是无效的变量名称。变量名称不能包含破折号。试试这个。

bg-color

此外,您可能希望从同一元素设置颜色,但您的意图不清楚。也许这就是你实际想要的东西:

$( ".html-element-color" ).each(function( index ) {

  var bgColor = $('.html-element-color').text();

   $(this).parent().find("div.wrapper").css( "background-color", bgColor ) ;

});