2015-07-22 35 views

回答

1

我把它分解成:

1)使用click事件處理(D3或jQuery的或純JavaScript)來讀取顏色值。

2)指定的顏色值,你已經使用.attr().style()繪製的形狀。

希望這個簡單的小提琴可能是沿着你的意圖的線路:http://jsfiddle.net/wg360abt/1/

注意,這不按照通常的輸入/更新/出模式。

1

此代碼應工作

<head> 
    <style> 
     #shape{ 
      border:1px solid; 
      width:50px; 
      height:50px; 
     } 
    </style> 
    <script> 
     function update(){ 
      var shape = document.getElementById("shape") 
      var col = document.getElementById("col") 
      shape.style.backgroundColor = col.value 
     } 
     document.addEventListener("input",function(e){ 
      if(e.target.id == "col"){ 
       update() 
      } 
     }); 
    </script> 
</head> 
<body> 
    <input type = "color" id = "col" /> 
    <div id = "shape"></div> 
</body> 
0

上述答案略有修改的方法。希望這可以幫助你:

<input type="color" name="favcolor" value="#cccccc" id="color_pick" onchange="setColor();"/>

在功能,您可以通過選擇設置顏色。

Full code:http://jsfiddle.net/d_dash/j5Lndom8/