【问题标题】:Apply Color Picker Selection to Div Font将颜色选择器选择应用于 Div 字体
【发布时间】:2018-04-12 07:53:17
【问题描述】:
  1. 如何将颜色选择器中选定的颜色应用到以下 div:#header、#subHeader、#button ?

  2. 如何隐藏 div #button 的边框,直到用户在<input id="text3" class="textInput"> 中键入文本

$(document).ready(function(){
    var div1 = $('#header')[0];

    $('#text1').bind('keyup change', function() {
        div1.innerHTML = this.value;
    }); 

    var div2 = $('#subHeader')[0];

    $('#text2').bind('keyup change', function() {
        div2.innerHTML = this.value;
    });

    var div3 = $('#button')[0];

    $('#text3').bind('keyup change', function() {
        div3.innerHTML = this.value;
    });
});


$(".basic").spectrum();
$(".override").spectrum({
    color: "yellow"
});
$(".startEmpty").spectrum({
    allowEmpty: true
});
.wrapper {
  padding: 10px;
  border: 2px solid black;
}

h2 {
  margin: 20px 0 5px;
  font-size: 16px;
  font-family: "helevtica", sans-serif;
  font-weight: 400;
}

#header {
  font-size: 60px;
  color: black;
  font-family: "helevtica", sans-serif;
  font-weight: 400;
  margin: 10px 0;
  padding: 20px;
}

#subHeader {
  font-size: 24px;
  color: black;
  font-family: "helevtica", sans-serif;
  font-weight: 300;
  margin: 10px 0;
  padding: 20px;
  display: block;
}

#button {
  font-size: 16px;
  color: black;
  font-family: "helevtica", sans-serif;
  font-weight: 400;
  margin: 30px 0;
  padding: 16px 24px;
  border: 2px solid #000;
  border-radius: 3px;
  text-align: center;
}
<link href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/226140/spectrum.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/226140/spectrum.js"></script>

<div class="wrapper">
<div id="header"></div> 
<div id="subHeader"></div>
<div id="button"></div>
</div>

<h2>Header</h2><input id="text1" class="textInput">
<h2>Subheader</h2><input id="text2" class="textInput">
<h2>Button</h2><input id="text3" class="textInput">
<h2>Color</h2>
<input type='text' class="basic" />

【问题讨论】:

  • 这个问题的格式是否充分?我不确定从哪里开始使用颜色选择器,所以我真的什么都尝试不了。

标签: javascript jquery html css color-picker


【解决方案1】:

这将改变您的#subHeader 颜色。

$(document).ready(function(){
    var div1 = $('#header')[0];

    $('#text1').bind('keyup change', function() {
        div1.innerHTML = this.value;
    }); 

    var div2 = $('#subHeader')[0];

    $('#text2').bind('keyup change', function() {
        div2.innerHTML = this.value;
    });

    var div3 = $('#button')[0];

    $('#text3').bind('keyup change', function() {
        div3.innerHTML = this.value;
        if(this.value.length > 0) {
            $('#button').css('display', 'block')
        } else {
           $('#button').css('display', 'none')
        }
    });
});


$(".basic").spectrum({
   change: function(color) {
       console.log(color.toHexString());
       $('#subHeader').css('color',color.toHexString());
       var s = '2px solid ' + color.toHexString();
       $('#button').css('border', s);
}
});
$(".override").spectrum({
    color: "yellow"
});
$(".startEmpty").spectrum({
    allowEmpty: true
});
.wrapper {
  padding: 10px;
  border: 2px solid black;
}

h2 {
  margin: 20px 0 5px;
  font-size: 16px;
  font-family: "helevtica", sans-serif;
  font-weight: 400;
}

#header {
  font-size: 60px;
  color: black;
  font-family: "helevtica", sans-serif;
  font-weight: 400;
  margin: 10px 0;
  padding: 20px;
}

#subHeader {
  font-size: 24px;
  color: black;
  font-family: "helevtica", sans-serif;
  font-weight: 300;
  margin: 10px 0;
  padding: 20px;
  display: block;
}

#button {
  display:none;
  font-size: 16px;
  color: black;
  font-family: "helevtica", sans-serif;
  font-weight: 400;
  margin: 30px 0;
  padding: 16px 24px;
  border: 2px solid #000;
  border-radius: 3px;
  text-align: center;
}
<link href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/226140/spectrum.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/226140/spectrum.js"></script>

<div class="wrapper">
<div id="header"></div> 
<div id="subHeader"></div>
<div id="button"></div>
</div>

<h2>Header</h2><input id="text1" class="textInput">
<h2>Subheader</h2><input id="text2" class="textInput">
<h2>Button</h2><input id="text3" class="textInput">
<h2>Color</h2>
<input type='text' class="basic" />

【讨论】:

  • 几乎!我添加了` $('#header').css('color',color.toHexString()); ` 和 ` $('#button').css('color',color.toHexString()); ` 但我仍然需要将颜色更改分配给按钮上的边框颜色。
  • 您需要在更改事件中添加它,就像我一样。如果您认为是正确的答案,请接受它,这样其他有相同问题的人就会知道这是一个好答案。
  • “将其添加到更改事件”看起来像这样: $('#button').css('color','border'color.toHexString()); ?这不起作用
  • 另外,有没有办法在按钮文本输入之前隐藏按钮边框?
  • 约翰尼我更新了答案,所以你必须更改按钮的css并添加display:none;作为默认值,如果按钮文本字段(#text3)的输入值长度大于0然后将显示更改为阻止。清楚吗?您还需要其他信息吗?
【解决方案2】:

添加这个:

$(".basic").spectrum({
    change: function(color) {
       console.log(color.toHexString());
       //add your code here
    }
});

然后您应该添加代码以根据所选颜色更改您想要的内容。如果这对您有帮助,或者您有任何其他问题,请告诉我。

【讨论】:

  • 通过“在此处添加您的代码”——我将在此处添加哪些代码?代码你举个例子?
  • 添加了一个新的答案,随着 subHeader 的变化,其他元素应该以同样的方式完成。现在还好吗?
  • 约翰尼让我知道现在是否还好 :) 或者您是否需要其他帮助。
猜你喜欢
  • 2021-08-09
  • 1970-01-01
  • 2012-04-09
  • 1970-01-01
  • 2012-06-20
  • 1970-01-01
  • 2012-06-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多