前端面试题
来源:互联网 发布:怎么给淘宝刷差评 编辑:程序博客网 时间:2024/05/22 04:24
要求:
<!-- alert(toRGB("#0000FF")); // 输出 rgb(0, 0, 255) -->
<!-- alert(toRGB("invalid")); // 输出 invalid; 【invalid 无效的; 不能成立的】 -->
<!-- alert(toRGB("#G00")); // 输出 #G00 ,因为 G 不是十六进制 -->
我感觉网上给的代码,不大对,我就自己写了一个。
<div id="div_1" style="width:50px;height:50px;background:red;"></div><script>//所以,首字符索引到 # 且 后面跟着正的字符串得小于7位,且里面没有字母大于 F 的function toRGB(color){var aaa = color;// aaa.substring(0,1).indexOf('#') 索引到了 输出 0 // #ffffff 7个,转RGB才好使, #ccc 4个 直接用可以,但是转为 RGB 就不可以var aaaLen = aaa.length; var count = 0;if( aaa.substring(0,1).indexOf('#') > -1 && ( aaaLen == 7 || aaaLen == 4 ) ){ //i=1,指从第二个数开始遍历,两个字符取一组for( var i=1;i<aaaLen;i++ ){var reg=/^[0-9a-fA-F]$/; //正则匹配 十六进制 的颜色if( reg.test( aaa.substring(i,i+1) ) ){count++;}}//console.log(count);//console.log(aaaLen - 1);if( count == aaaLen - 1 ){if( count == 6 ){var hh=""var kk=[];for(var i=1;i<color.length;i+=2){ /*substr() 方法可在字符串中抽取从 start 下标开始的指定数目的字符。语法:stringObject.substr(start,length)substring() 方法用于提取字符串中介于两个指定下标之间的字符。语法: stringObject.substring(start,stop)*/ //hh=color.substring(i,i+2); hh=color.substr(i,2); var cc=parseInt(hh,16); //转 16进制数 kk.push(cc);}var oDiv =document.getElementById('div_1');oDiv.style.background = "RGB("+kk+")";console.log( color +' is color ');return ( color+ " to "+"RGB("+kk+")");}else{console.log( color +' is color but cannot toRGB ');return color;}}else{console.log( color +' is not color ');return color;}}else{console.log( color +' is not color ');return color;}}console.log(toRGB("#0011FF")); //输出 //#0011FF is color//#0011FF to RGB(0,17,255)console.log(toRGB("invalid")); //输出 //invalid is not color//invalidconsole.log(toRGB("#chc")); //输出 //#chc is not color //#chcconsole.log(toRGB("#ccc")); //输出 //#ccc is color but cannot toRGB //#ccc</script>
阅读全文
0 0
- 前端面试题
- 前端面试题
- 前端面试题整理
- 前端面试题
- web前端面试题
- 前端开发面试题
- 收录:前端面试题
- 前端面试题
- Web前端面试题
- 前端面试题
- 阿里巴巴 前端面试题
- 前端各种面试题
- web前端面试题
- web前端面试题
- 前端面试题
- web前端面试题
- 一道前端面试题
- 前端面试题
- springboot 文件上传
- 23种设计模式(16)-迭代子模式
- 网络模型--Densely Connected Convolutional Networks
- ajax用户名验证和登录方法的整合
- Java三大特性
- 前端面试题
- app渠道统计,你认真了吗
- 如何查找JVET-H.266提案和下载参考软件
- ios-代码实现autolayout
- Vue之生命周期
- C# 调用WebApi
- RxJava + Retrofit 结合使用
- JQ的便利数组中的各个字符串
- poj 1082 Calendar Game