我使用单选按钮有以下表格:
<div class="attributes">
<div class="attribute attribute-1 odd">
<div class="form-item">
<label>Color: <span class="form-required" title="This field is required.">*</span></label>
<div class="form-radios"><div class="form-item" id="edit-attributes-1-7-wrapper">
<label class="option" for="edit-attributes-1-7">
<input type="radio" id="edit-attributes-1-7" name="attributes[1]" value="7" class="form-radio" /> Black | 0x000000</label>
</div>
<div class="form-item" id="edit-attributes-1-6-wrapper">
<label class="option" for="edit-attributes-1-6">
<input type="radio" id="edit-attributes-1-6" name="attributes[1]" value="6" class="form-radio" /> Blue | 0x5E79A4</label>
</div>
<div class="form-item" id="edit-attributes-1-5-wrapper">
<label class="option" for="edit-attributes-1-5">
<input type="radio" id="edit-attributes-1-5" name="attributes[1]" value="5" class="form-radio" /> Red | 0xC33438</label>
</div>
</div>
</div>
我正在尝试用带有相关颜色的正方形替换“Color | 0xxxxxxx”部分。基本上,我需要阅读“Color | 0xxxxxxx”,解析它以提取颜色的十六进制值,并用<div style="height :20px;width:20px;background-color:0xxxxxx"></div>
之类的div替换整个文本
有人对此有任何见解吗?
答案 0 :(得分:3)
首先,不要嵌套标签,而是将其放在输入端:
<input type="radio" id="edit-attributes-1-5"
name="attributes[1]" value="5" class="form-radio" /><label
class="option" for="edit-attributes-1-5">Red | 0xC33438</label>
然后使用jquery:
$("label[for^='edit-attributes']").each(
// select labels whose "for" begin with "edit-attributes"
function() {
var text = $(this).html();
// css color begin with #, not 0x
color = '#' + text.substr(text.indexOf("| 0x")+4);
$(this).html('<div class="optionsquare" style="background-color:' +
color + '">');
});
的CSS:
.optionsquare {
display:inline-block;
height:20px;
width:20px;
}
答案 1 :(得分:0)
有了这个:
$("label.option").each(function(){
var lithex = $(this).text().split("|");
var hex = $.trim(lithex[1]);
$(this).text("").append('<div style="height :20px;width:20px;background-color:'+hex+'"></div>');
});
希望这会有所帮助。干杯
答案 2 :(得分:0)
$(document).ready(function()
{
$('input[type="radio"].form-radio').each(
function()
{
var aa = $(this).next().text().split('|');
var NewDiv = $('<div></div>');
NewDiv.css({ backgroundColor: aa[1].substring(3, aa[1].length), height: '20px',width: '20px' });
$(this).parents('.form-item:first').append(NewDiv);
});
});
答案 3 :(得分:0)
我终于找到了永久删除文本并用span替换它的方法,无需更改标记。
$(document).ready(function() {
$(".form-radio").each(function() {
var label = $(this).parent();
label.wrapInner('<span class="will-be-deleted" />').find("input").appendTo(label);
var text = label.find("span").text();
var color = /0x[a-f0-9]{6}/i.exec(text)[0];
var icon = $("<span />").css({
"margin-left": 4,
"padding-left": 16,
"background-color": color.replace(/^0x/, "#")
})
label.find("span").remove();
label.append(icon);
});
});