我正在处理我的 jquery 脚本,以找到带有颜色的样式和我想要找到的 rgb 以将其替换为 #444444
。我在查找样式颜色时遇到了问题,因为当我尝试这样做时:
$('#text_editor').find('[style*="color: rgb(68, 68, 68)]"').css('color', '#444444');
它不会让我找到带有颜色和 rgb 的样式,因为它不会替换它。
我也试过这个:
$('#text_editor').find('color: rgb(68, 68, 68)').css('color', '#444444');
还有这个:
document.execCommand('foreColor', false, '#444444');
$('#text_editor').find("span[style*='color: rgb(68, 68, 68)']").css('color', '#444444');
这没有什么不同,因为我不断得到这个:
<span style="color: rgb(68, 68, 68);">sdgsdgsdgsdgsdg</span>
这是我想要实现的:
<span style="color: #444444;">sdgsdgsdgsdgsdg</span>
这是 html:
<div id="text_editor" class="editor_message" hidefocus="false" aria-label="Message Body" g_editable="true" role="textbox" aria-hidden="true" aria-multiline="true" contenteditable="true" tabindex="1" style="direction: ltr; height: 500px; width: 100%; padding-left: 25px; padding-top: 18px; font-size: 16px; border-bottom: 1px solid #d1d1d1; border-right: 1px solid #d1d1d1; border-left: 1px solid #d1d1d1; overflow-y: auto;" itacorner="6,7:1,1,0,0">
<span style="color: rgb(68, 68, 68);">sdgsdgsdgsdgsdg</span>
</div>
完整代码如下:
$(document).on('click', '#color-picker1', function(e) {
$('#color-picker' + colorpicker_number + ' div').removeClass('tick-color');
$('#color-picker' + colorpicker_number).removeClass('color-picker-border');
$('#color-picker1 div').addClass('tick-color');
$('#color-picker1').addClass('color-picker-border');
colorpicker_number = 1;
alert("time to change the color 6");
document.execCommand('styleWithCSS', 0, true);
document.execCommand('foreColor', false, '#444444');
//$('#text_editor').find('[style*="color: rgb(68, 68, 68);"],color').css('color', '#444444');
$('#text_editor').find('[style*="color: rgb(68, 68, 68)]"').css('color', '#444444');
if ($('#toolbar_text_color').css('display') == 'block') {
$('#toolbar_text_color').hide();
}
$('#text_editor').focus();
});
你能给我一个例子吗,在我可以使用 css 将它替换为 #444444
之前,我如何找到我想要找到的 rgb 颜色的样式?
谢谢。
最佳答案
您在 .find()
中的错误位置结束了 "
$('#text_editor').find('[style*="color: rgb(68, 68, 68)]"') // Wrong
$('#text_editor').find('[style*="color: rgb(68, 68, 68)"]') // Right
但是,这不是您的主要问题。问题是 colors
计算值会使用它们的 rgb
或 rgba
值自动显示给您,as per the spec - 所以这是你无法真正避免的标准行为,这些值总是会被计算回 rgb
或 rgba
Computed value: If the value is translucent, the computed value will be the rgba() corresponding one. If it isn't, it will be the rgb() corresponding one. The transparent keyword maps to rgba(0,0,0,0)
关于javascript - JQuery 查找 span 样式颜色以替换为颜色哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59571573/