gpt4 book ai didi

javascript - 如何获得 parent 的值(value)

转载 作者:行者123 更新时间:2023-11-30 09:38:34 25 4
gpt4 key购买 nike

如您所见,我有多个 .box div 并且我的 .box div 具有我需要的 data-item-color 属性获取 data-item-color 值来更改我的 .box css 属性 (.line,.tinyline,h6,p),很快:

如果我的 data-item-colorred 值比我的 .line,h6,p.box div

里面的红色

demo link

$(document).ready(function() {

});
h6,
p,
span {
padding: 0;
margin: 0;
}

.box {
width: 200px;
padding: 20px;
float: left;
margin: 10px;
}

.line:before {
content: "";
width: 100%;
height: 5px;
background: #000;
display: block;
}

.tinyline:after {
content: "";
width: 100%;
height: 2px;
background: #000;
display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="box" data-item-color="red">
<span class="line"></span>
<h6>RED</h6>
<p>red text...
<span class="tinyline"></span>
</p>
</div>

<div class="box" data-item-color="blue">
<span class="line"></span>
<h6>BLUE</h6>
<p>Blue text..
<span class="tinyline"></span>
</p>
</div>

最佳答案

看看这个。

我遍历了每个 div 并将其颜色应用于其子项。

$(document).ready(function() {
$("div.box").each(function(){
$(this).find("*").css("color",$(this).data("item-color"));
$(this).find(".line,.tinyline").css("background-color",$(this).data("item-color"));
});
});
h6,
p,
span {
padding: 0;
margin: 0;
}

.box {
width: 200px;
padding: 20px;
float: left;
margin: 10px;
}

.line {
content: "";
width: 100%;
height: 5px;
background: #000;
display: block;
}

.tinyline {
content: "";
width: 100%;
height: 2px;
background: #000;
display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="box" data-item-color="red">
<span class="line"></span>
<h6>RED</h6>
<p>red text...
<span class="tinyline"></span>
</p>
</div>

<div class="box" data-item-color="blue">
<span class="line"></span>
<h6>BLUE</h6>
<p>Blue text..
<span class="tinyline"></span>
</p>
</div>

关于javascript - 如何获得 parent 的值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42298910/

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