gpt4 book ai didi

Jquery 和十六进制颜色?

转载 作者:太空宇宙 更新时间:2023-11-03 21:29:26 25 4
gpt4 key购买 nike

如果我从 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 ) ;

});

最佳答案

bg-color 是无效的变量名。变量名不能包含破折号。试试这个。

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

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

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

});

此外,您可能想从同一元素设置颜色,但您的意图在您的帖子中并不清楚。也许这就是您实际上想要的:

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

var bgColor = $(this).text();

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

});

关于Jquery 和十六进制颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30533655/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com