我想做一些有点笨拙的事情,我的伪代码不工作,但应该解释我想做什么。我有 4 个单选按钮组,在 6 个独立的 div 中,所以总共有 24 个单选按钮。每个 div 都是一种特定的背景颜色。 When a radio button within a particular div is selected, I need to grab the colour of the div the selected radio button is within, and have it stored in a variable ready to apply it to an element that'll be created next...
$("#voucher_selection_container input:radio").change(function(){
//var color = $(this).parent("div").css("background-color");
//alert(color);
$("#voucher_customisation_container").slideDown(600);
//$("#voucher_customisation_container .voucher_box").css("background-color", color);
});
如有任何帮助,我们将不胜感激。 :) 谢谢。
最佳答案
尝试使用 closest()
在 DOM 树中查找下一个匹配元素
var color = $(this).closest("div").css("background-color");
关于jQuery 获取父元素的 css 背景颜色值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10333930/