<!DOCTYPE html>
<html>
<body>
<p>请更改颜色选择器的颜色,然后单击下面的按钮。</p>
颜色选择器:<input type="color" id="myColor" value="#ff0080">
<p>请注意,当您更改颜色选择器的值时,默认值不受影响。</p>
<button type="button" onclick="myFunction()">试一试</button>
<p><b>注意:</b>Internet Explorer 11 及更早版本或 Safari 9.1 及更早版本不支持 type="color"。</p>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("myColor");
var defaultVal = x.defaultValue;
var currentVal = x.value;
if (defaultVal == currentVal) {
document.getElementById("demo").innerHTML = "默认值和当前值相同:"
+ x.defaultValue + " 和 " + x.value
+ "<br>更改颜色选择器的颜色以查看差异!";
} else {
document.getElementById("demo").innerHTML = "默认值是:" + defaultVal
+ "<br>新的当前值是:" + currentVal;
}
}
</script>
</body>
</html>