JS實現(xiàn)簡單面向?qū)ο蟮念伾x擇器實例
本文實例講述了JS實現(xiàn)簡單面向?qū)ο蟮念伾x擇器。分享給大家供大家參考,具體如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Untitled Document</title>
</head>
<body>
<script type="text/JavaScript">
<!--
var colorPicker = function(idStr){
this.colorPool = ["#000000","#993300","#333300","#003300","#003366","#000080","#333399","#333333","#800000","#FF6600","#808000","#008000","#008080","#0000FF","#666699","#808080","#FF0000","#FF9900","#99CC00","#339966","#33CCCC","#3366FF","#800080","#999999","#FF00FF","#FFCC00","#FFFF00","#00FF00","#00FFFF","#00CCFF","#993366","#CCCCCC","#FF99CC","#FFCC99","#FFFF99","#CCFFCC","#CCFFFF","#99CCFF","#CC99FF","#FFFFFF"];
this.initialize(idStr);
}
colorPicker.prototype = {
initialize: function(idStr){
var count=0;
var html = '';
var self = this;
html+= '<table cellspacing="5" cellpadding="0" border="2" bordercolor="#000000" style="cursor:pointer;background:#ECE9D8" mce_style="cursor:pointer;background:#ECE9D8" >';
// html+= '<tr><td align="center" colspan="8" width="160" height="20" id="currentColor" bgcolor="#ffffff">當前顏色</td></tr>';
for(i=0;i<5;i++)
{
html+= "<tr>";
for(j=0;j<8;j++)
{
html+= '<td align="center" width="20" height="20" style="background:'+ this.colorPool[count]+'" mce_style="background:'+ this.colorPool[count]+'" unselectable="on"> </td>';
count++;
}
html+= "</tr>";
}
html+= '</table>';
this.trigger = document.getElementById(idStr);
this.div = document.createElement('div');
this.div.innerHTML = html;
var tds = this.div.getElementsByTagName('td');
for(var i=0,l=tds.length;i<l;i++){
tds[i].onclick = function(){
self.setColor(this.style.backgroundColor);
}
}
this.div.id = 'myColorPicker';
this.trigger.parentNode.appendChild(this.div);
this.div.style.position = 'absolute';
this.div.style.left = this.trigger.offsetLeft + 'px'
this.div.style.top = (this.trigger.clientHeight + this.trigger.offsetTop)+ 'px';
//this.hide();
this.trigger.onclick = function(){
if(self.div.style.display == 'none'){
self.show();
return false;
}else{
self.hide();
return false;
}
}
},
setColor : function(c){
this.hide();
document.getElementById('demo').style.backgroundColor = c //proEditor.setColor(c); //自己定義函數(shù)決定setColor的功能
},
hide : function(){
this.div.style.display = 'none'
},
show : function(){
this.div.style.display = 'block'
}
}
// -->
</script>
<div >
<a href="#" mce_href="#" onclick="initColorPicker();return false" id="demo" style="position:absolute;left:200px">顏色選擇</a>
</div>
<script type="text/javascript">
<!--
function initColorPicker(){
picker = new colorPicker('demo');
}
// -->
</script>
</body>
</html>
對于JS顏色工具感興趣的朋友可參看本站在線工具:
更多關于JavaScript相關內(nèi)容可查看本站專題:《JavaScript切換特效與技巧總結》、《JavaScript查找算法技巧總結》、《JavaScript動畫特效與技巧匯總》、《JavaScript錯誤與調(diào)試技巧總結》、《JavaScript數(shù)據(jù)結構與算法技巧總結》、《JavaScript遍歷算法與技巧總結》及《JavaScript數(shù)學運算用法總結》
希望本文所述對大家JavaScript程序設計有所幫助。
相關文章
一文教你實現(xiàn)JavaScript防抖優(yōu)化代碼
在我們前端編程中,假如我們要給后端發(fā)送請求,萬一手抖多點了幾次,多發(fā)送了幾遍怎么辦,那就得用防抖處理,下面小編就來教大家如何實現(xiàn)防抖吧2023-11-11
Omi v1.0.2發(fā)布正式支持傳遞javascript表達式
這篇文章主要介紹了Omi v1.0.2發(fā)布正式支持傳遞javascript表達式,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-03-03
理運用命名空間讓js不產(chǎn)生沖突避免全局變量的泛濫
為了避免變量之間的覆蓋與沖突,可以生成命名空間,命名空間是一種特殊的前綴,在不同的匿名函數(shù)中,根據(jù)功能聲明一個不同的命名空間2014-06-06
BootStrap中Datepicker控件帶中文的js文件
bootstrap-datepicker 是一個非常優(yōu)秀的時間選擇插件。這篇文章主要介紹了bootstrap-datepicker帶中文的js文件的相關資料,需要的朋友可以參考下2016-08-08
uni-app實現(xiàn)web-view圖片長按下載解決方案
uniapp的web-view中圖片無法長按保存,IOS下是正常的,但是Android下長按無反應,這篇文章主要介紹了uni-app實現(xiàn)web-view圖片長按下載解決方案,需要的朋友可以參考下2023-09-09

