Archive for the ‘JavaScript’ tag
自适应-css & css opacity效果
1,页面中这样效果:
xxxxxx xxxxxxx
左侧内容是变化的,长度不固定;右侧内容是死链接; 并且外层宽度是固定的。
前两天产品说存在bug,已上线, 要尽快修复. 当时临时固定了左侧的宽度, 解决了当天的问题.
今天内容少了, 长度小了, 问题又回来了(但并不严重); 下班正好有时间进行了修正.在此备忘;
<span class="wrap"><a href="#">xxxxxxxxxxxxxxxxxxxxxx</a></span><span class="star">xxxxxxx</span>
.mod{overflow: hidden;height: 24px;line-height: 24px;} .wrap,.time{float: left;} .wrap{width: 100%;} .time{width: 96px;margin-left:-96px;} .wrap a{display: block;margin: 0 100px 0 10px;}
2, 在设置中心有a不选中, 则b选项, B,C内容块, 置灰效果;(BC块里面的事件全部取消, hover,click等等)
方法:
a末选时, 设置b的disable为true即可, 同时给B,C绑定一个data,为true或者false;
B,C块根据data值, 设置自身的opacity, 主要是提示用户,这里不可点击, 无任务操作;
里面的事件就简单了, 在执行操作时, 判断data值;如果是设置的值, 则不进行任务操作即可;
blur事件兼容问题
在使用Blur事件时,遇到这样问题:
当移出焦点,并且触发阻止默认行为的事件时,blur会失效(chrome/firefox; IE6正常, 其它未测试)。
HTML代码:
<input name="article" id="article" /> <div id="un" style="height: 120px;background: #666;"></div>
JavaScript代码:
// 测试使用, 命名随意 var test = document.getElementById('article'), div = document.getElementById('un'); test.onblur = function(){ // IE6会弹出'blur';chrome/firefox 无反应. alert('blur');//console.log('blur'); } div.onmousedown = function(e){ e = e || window.event; if(e.preventDefault){ e.preventDefault(); }else{ e.returnValue = false; } }
做为备忘!
js控制提示信息
看到我们的浏览器中,在保存设置时,会提示保存信息成功,n秒后提示消失.
今天在看blog时,正好又看到豪情哥在实现显示层3s后隐藏类似效果. 下面自己也实现下:
var $ = function(elem){
return document.getElementById(elem);
};
var show = function(elem){
elem.style.display = 'block';
};
var hide = function(elem){
elem.style.display = 'none';
};
var timeLayer = function(elem, time){
if(typeof elem !== 'object') {
return false;
}
time = time || 2;
show(elem);
if(timer){
clearTimeout(timer);
}
var timer = setTimeout(function(){
hide(elem);
},time*1000);
};
$('test_btn').onclick = function(){
timeLayer($('show_layer'));
};
写成简单的demo后,发现太过简单。想扩展下,最后有个timeLayer函数, 再想… 缺的东西越来越多… 好了,打住。
满足需求,适合自己的才是重要的。生活中也是一样….
JavaScript 实现getAttribute和text函数
var walk_the_DOM = function walk(node, func){
func(node);
node = node.firstChild;
while (node){
walk(node, func);
node = node.nextSibling;
}
};
var getElementByAttribute = function(att, value){
var results = [];
walk_the_DOM(document.body, function(node){
var actual = node.nodeType === 1 && node.getAttribute(att);
if(typeof actual === 'string' && (actual === value || typeof value !== 'string')){
results.push(node);
}
});
return results;
};
function text (elem) {
var results = [];
walk_the_DOM(elem, function(node){
var actual = node.nodeType === 3 && node.nodeValue;
if(typeof actual === 'string'){
results.push(actual);
}
});
return results.join('');
}
上面两个函数来自《JavaScript the good parts》;
js文件, .php后缀
今天QQ群里说起了内容为JavaScript的后缀为.php的php文件,唤起我两个月之前遇到的问题: .php文件不能像JavaScript一样缓存。
直接分析:
- .php文件类似.html文件,每次请求都会直接向服务器要数据(或内容),不缓存。
- .js?t=xxx; 第二次请求时会缓存。除非每次请求,它的t都不相同。
- .js?t= (xxx); 每次t不相同,防止js文件缓存。
在项目中遇到的.php后缀的:
- 是后台A程序员,为了方便直接写php程序在文件里;
- 是不缓存,比如天气预报,数据在一天内会变上几次。
遇到以.js?t=20101011976结尾的:
- JavaScript文件手动小更新,解决CDN上文件缓存问题,大版本不升级。
最后一种, 么遇到过…