简体   繁体   English

是否可以在数据属性中使用json内容应用CSS规则?

[英]Is it possible to apply css rules using json content in a data attribute?

Is it possible to put green background on the spans that have an id represented in the div? 是否可以在具有div代表ID的跨度上放置绿色背景?

//Edit. //编辑。 If it makes it easier, just using the keys in the div attribute would work too. 如果使它变得更容易,则只需使用div属性中的键也可以。 data-ids='["1", "2"]' or even data-ids="1,2" . data-ids='["1", "2"]'甚至data-ids="1,2" Not sure what's possible. 不知道有什么可能。 If not, is there any clever JS/jQuery implementation that could help out? 如果没有,是否有任何聪明的JS / jQuery实现可以帮上忙?

<div data-ids='{"1":"Name A", "2":"Name B"}'>
    <span data-id="1">This should have green background</span>
    <span data-id="2">This should have green background</span>
    <span data-id="3">This should have no background</span>
</div>

Dummy code: 虚拟代码:

div[data-ids=*] span[data-id=*] {
    background: green;
}

This loops through divs, then loops through data ids of those divs and then adds a class to the appropriate children span 这将遍历div,然后遍历这些div的数据ID,然后将一个类添加到适当的子跨度

 $("div[data-ids]").each(function(){ let obj = $(this); $.each($(this).data("ids"),function(k,v){ $(obj).find("span[data-id='" + k + "']").addClass("highlight"); }); }); 
 .highlight{ background: green; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div data-ids='{"1":"Name A", "2":"Name B"}'> <span data-id="1">This should have green background</span> <span data-id="2">This should have green background</span> <span data-id="3">This should have no background</span> </div> 

You can't dynamically set styles using CSS. 您不能使用CSS动态设置样式。 If you must do it, you need to manually type all possibilities out. 如果必须这样做,则需要手动键入所有可能性。

For example, if you need green background for <span> with id == 1 or id == 2 , you need to write your CSS like this: 例如,如果您需要id == 1id == 2 <span>绿色背景,则需要这样编写CSS:

 div[data-ids] span[data-id="1"], div[data-ids] span[data-id="2"] { background: #0f0; } 
 <div data-ids='{"1":"Name A", "2":"Name B"}'> <span data-id="1">This should have green background</span> <span data-id="2">This should have green background</span> <span data-id="3">This should have no background</span> </div> 

You have to use JS or jQuery to do what you need to do: 您必须使用JS或jQuery来完成您需要做的事情:

JavaScript (ES6 for simplicity) - see comments for explanation JavaScript(为简化起见,ES6) - 参见注释以获取解释

 // Get the div with `data-ids` attribute const div = document.querySelector('div[data-ids]'); // Get the data in `data-ids` const ids = div.dataset.ids; // Parse the string data into Object const data = JSON.parse(ids); // Loop over the keys (id) of the data // Selecting matching span to style them. for (const id in data){ const spans = document.querySelectorAll(`span[data-id="${id}"]`); spans.forEach(span => { span.style.background = '#0f0'; }); } 
 <div data-ids='{"1":"Name A", "2":"Name B"}'> <span data-id="1">This should have green background</span> <span data-id="2">This should have green background</span> <span data-id="3">This should have no background</span> </div> 

jQuery (ES6 for simplicity) - see comments for explanation jQuery(为简化起见,ES6) - 请参阅注释以获取解释

 // Get the div with `data-ids` attribute const $div = $('div[data-ids]'); // Get the `ids` from attribute. // jQuery automatically parse string data into JSON if valid. const data = $div.data('ids'); // Loops through the ids and set matching span background color. $.each(data, function(id){ $(`span[data-id="${id}"]`).css('background', '#0f0'); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div data-ids='{"1":"Name A", "2":"Name B"}'> <span data-id="1">This should have green background</span> <span data-id="2">This should have green background</span> <span data-id="3">This should have no background</span> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM