js调试函数
参考php的debug函数修改了一下JS版的。 firebug 下使用控制台 ie使用 alert弹出
/**
* 内部调试
**/
function debug(v){
if(window.console && window.console.log){
window.console.log(v);
}else{
alert(v);
}
}
同事提供的题目(Taobao)
alert(toRGB("#0000FF")); //返回rgb(0,0,255)
alert(toRGB("#00F")); //返回rgb(0,0,255)
alert(toRGB("#FF0000")); //返回rgb(255,0,0)
alert(toRGB("taobao")); //返回rgb(taobao)
alert(toRGB("#G00")); //返回rgb(#G00)
function toRGB(color){
//第有一字符有#号
if(color.indexOf('#')==0){
//判断长度
if(color.length==4){
//取出字母
r = color.match(/([\da-z])/ig);
}else{
//取出字母
r = color.match(/([\da-z]{2})/ig);
}
//返回
return 'rgb('+formatHEX(r) +')';
}else{
return 'rgb('+color+')';
}
}
//转化色彩数组转为10进制
function formatHEX(x){
var html=[];
for(i in x){
html[i] = parseInt(x[i], 16);
if(isNaN(html[i])){
return '#'+x.join('');
}
}
return html.join(',');
}
编码问题导致Javascript报错.
问题: 报JS标签没有关闭.
原因: JS没有修改过且线上版本也是正常的,但在测试环境下.
解决: 跟踪了很久, 变量, 常量, 数组是正常了. 后来看到页面上JS输出的内容乱码.
这里想到可能是 页面编码 与 JS编码不同导致的问题.
程序的页面是GB2312的, 但我们写的JS都是UTF8的...
jquery 处理表单元素得到焦点与失去焦点
/*处理表单元素得到焦点与失去焦点*/
jQuery('.input_text').focus(function(){
var i = jQuery(this).attr('init_val');
var t = jQuery(this).attr('user_val');
if(i==undefined){
//记录初始值
jQuery(this).attr('init_val',jQuery(this).val());
jQuery(this).val('');
}else if(i==jQuery(this).val()){
jQuery(this).val('');
}else{
jQuery(this).val(t);
}
jQuery(this).css('color','#333');
}).blur(function(){
i = jQuery(this).attr('init_val');
if(jQuery(this).val()==''|| jQuery(this).val()==i){
jQuery(this).css('color','#bbb');
jQuery(this).val(jQuery(this).attr('init_val'));
}else{
jQuery(this).attr('user_val',jQuery(this).val());
jQuery(this).css('color','#333');
}
});
用于解决iframe自适应高度
function reinitIframe(){
var iframe = document.getElementById("frame_content");
try{
var bHeight = iframe.contentWindow.document.body.scrollHeight;
var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
var height = Math.max(bHeight, dHeight);
iframe.height = height;
}catch (ex){}
}
window.setInterval("reinitIframe()", 1000);
在新浪站上看到的不错,收藏下.
今天写的一个练习的js类模式
suspendcode="" document.write(suspendcode); var hp = { lastScrollY : 0, byID : function(_id){ return document.getElementById(_id);}, init : function(id){ var st = document.documentElement.scrollTop || document.body.scrollTop; percent=.1*(st-this.lastScrollY); if(percent>0){ percent=Math.ceil(percent); }else{ percent=Math.floor(percent); } this.byID(id).style.top=parseInt(this.byID(id).style.top)+percent+"px"; this.lastScrollY=this.lastScrollY+percent; return id; }, generate : function(){ }, start : function(id){ window.setInterval("hp.init(’"+id+"’)",1); } }; #hp.start(’lovexin1′);
attachEvent()/addEventListener()方法
用attachEvent()/addEventListener()方法添加触发事件2007-05-12 15:11在近来的工作中,用到了attachEvent方法,该方法可以为某一事件附加其它的处理事件,有时候可能比较有用,这里将其基本用法总结一下。
其语法可以查看《DHTML手册》,里面有详细的说明,这里贴一个例子,该例子来自互联网:
document.getElementById("btn").onclick = method1;
document.getElementById("btn").onclick = method2;
document.getElementById("btn").onclick = method3;
如果这样写,那么将会只有medhot3被执行
写成这样:
var btn1Obj = document.getElementById("btn1");
//object.attachEvent(event,function);
btn1Obj.attachEvent("onclick",method1);
btn1Obj.attachEvent("onclick",method2);
btn1Obj.attachEvent("onclick",method3);
执行顺序为method3->method2->method1
如果是Mozilla系列,并不支持该方法,需要用到addEventListener
var btn1Obj = document.getElementById("btn1");
//element.addEventListener(type,listener,useCapture);
btn1Obj.addEventListener("click",method1,false);
btn1Obj.addEventListener("click",method2,false);
btn1Obj.addEventListener("click",method3,false);
执行顺序为method1->method2->method3
JavaScript中confirm的用法
configrm 用于删除或者其他时提示返回 t , f
例
if(window.confirm("确定删除吗?")){
$.get("../member/action.php?act=del", { snapID: id},
function(data){
if(data==1){
alert('success');
removeNode1(obj.parentNode.parentNode);
}else{
alert('error');
}
}
);
}
return false;
动态给ifram添加内容,兼容ie和FF
第一次知道,还可以这样写
<iframe width='100%' height='100%' name='boot' id='boot' src='' frameborder='0' scrolling='no'></iframe>
<SCRIPT LANGUAGE="JavaScript">
<!--
var iframe = window.frames['boot'];
iframe.document.open();
iframe.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n');
iframe.document.write('<html xmlns="http://www.w3.org/1999/xhtml">\n');
iframe.document.write('<head>\n');
iframe.document.write('<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />\n');
iframe.document.write('</head>\n');
iframe.document.write('<body>\n');
iframe.document.write('请等待...');
iframe.document.write('</body>\n');
iframe.document.write('</html>\n');
iframe.document.close();
//-->
</SCRIPT>
javascript与php类型转换
1. 字符串转换为整型
//php
intval(); //php
floatval();//php浮点
strval();
//javascript
toString(); //转化为整型
parseInt(); //js 转化为整型
parseFloat(); //转化为浮点
Number(); //js, 这个会自动转

