将实际颜色传递给js输入中的值
Pass actual color to value in input on js
我需要将颜色值动态传递给 <input>
,这样我就可以将 CSS 复制到剪贴板。有问题的颜色是当您更改按钮的 bg 颜色时自动更改的颜色。它始终为黑色或白色,并且不会自行传递给 --placeholdtext
。现在它被复制总是白色的。我使用 http://jscolor.com/。我认为该变量存在于 jscolor.js 文件中的某处,无法找到并使用。
我现在的代码:
function copyToClipboard(element) {
let currenttextColor = $(".jscolor").val();
let currenttextStyle = $(element).text();
let newtextStyle = currenttextStyle.replace('--placeholdtext', "#"+currenttextColor);
document.getElementById('myField').value = currenttextColor;
var $tempt = $("<input>");
$("body").append($tempt);
$tempt.val(newtextStyle).select();
document.execCommand("copy");
$tempt.remove();
}
function update(jscolor) {
document.getElementById('button_cont').style.color = '#' + jscolor;
}
#button_cont {
color: --placeholdtext;
font-size: 18px;
text-decoration: none;
padding: 10px;
/* border: 1px solid #ffffff; */
border-radius: 5px 5px 5px 5px;
-moz-border-radius: 5px 5px 5px 5px;
-webkit-border-radius: 5px 5px 5px 5px;
display: inline-block;
transition: all 0.4s ease 0s;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jscolor/2.0.4/jscolor.min.js"></script>
<a href="#" id="button_cont" >Call to action</a>
<button class="jscolor{valueElement:'valueInput', styleElement:'button_cont'}" >
Pick a color
</button>
<input id="valueInput" value="ed3330">
<button onclick="copyToClipboard('style')">Copy button</button></div></div>
<input type="text" id="myField" class="jscolor" onchange="update(this.jscolor);" style="display: none;" />
如您所见,我使用 document.getElementById('myField').value = currenttextColor;
将带有颜色的值插入到代码下方的 <input>
中,但我找不到所需的黑色或白色值变量。
<script type="text/javascript">
function copyToClipboard(element) {
var actualColor = window.getComputedStyle(document.getElementById('button_cont')).getPropertyValue('color');
document.getElementById('myField').value = actualColor;
let currenttextColor = $(".jscolor").val();
let currenttextStyle = $(element).text();
let newtextStyle = currenttextStyle.replace('--placeholdtext', currenttextColor);
var $tempt = $("<input>");
$("body").append($tempt);
$tempt.val(newtextStyle).select();
document.execCommand("copy");
$tempt.remove();
}
</script>
我需要将颜色值动态传递给 <input>
,这样我就可以将 CSS 复制到剪贴板。有问题的颜色是当您更改按钮的 bg 颜色时自动更改的颜色。它始终为黑色或白色,并且不会自行传递给 --placeholdtext
。现在它被复制总是白色的。我使用 http://jscolor.com/。我认为该变量存在于 jscolor.js 文件中的某处,无法找到并使用。
我现在的代码:
function copyToClipboard(element) {
let currenttextColor = $(".jscolor").val();
let currenttextStyle = $(element).text();
let newtextStyle = currenttextStyle.replace('--placeholdtext', "#"+currenttextColor);
document.getElementById('myField').value = currenttextColor;
var $tempt = $("<input>");
$("body").append($tempt);
$tempt.val(newtextStyle).select();
document.execCommand("copy");
$tempt.remove();
}
function update(jscolor) {
document.getElementById('button_cont').style.color = '#' + jscolor;
}
#button_cont {
color: --placeholdtext;
font-size: 18px;
text-decoration: none;
padding: 10px;
/* border: 1px solid #ffffff; */
border-radius: 5px 5px 5px 5px;
-moz-border-radius: 5px 5px 5px 5px;
-webkit-border-radius: 5px 5px 5px 5px;
display: inline-block;
transition: all 0.4s ease 0s;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jscolor/2.0.4/jscolor.min.js"></script>
<a href="#" id="button_cont" >Call to action</a>
<button class="jscolor{valueElement:'valueInput', styleElement:'button_cont'}" >
Pick a color
</button>
<input id="valueInput" value="ed3330">
<button onclick="copyToClipboard('style')">Copy button</button></div></div>
<input type="text" id="myField" class="jscolor" onchange="update(this.jscolor);" style="display: none;" />
如您所见,我使用 document.getElementById('myField').value = currenttextColor;
将带有颜色的值插入到代码下方的 <input>
中,但我找不到所需的黑色或白色值变量。
<script type="text/javascript">
function copyToClipboard(element) {
var actualColor = window.getComputedStyle(document.getElementById('button_cont')).getPropertyValue('color');
document.getElementById('myField').value = actualColor;
let currenttextColor = $(".jscolor").val();
let currenttextStyle = $(element).text();
let newtextStyle = currenttextStyle.replace('--placeholdtext', currenttextColor);
var $tempt = $("<input>");
$("body").append($tempt);
$tempt.val(newtextStyle).select();
document.execCommand("copy");
$tempt.remove();
}
</script>