firefox 用 document.getSelection()。
423423 53 53463464
if(document.selection){
document.onmouseup=function(){alert(document.selection.createRange().text)}
}else{
document.onmouseup=function(){alert(document.getSelection())}
}
[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]
参考资料:
测试如下,请用不同的浏览器来查看效果
属性选择器(attribute selector)
p[id] 表示选择所有带有 id 属性的 p 标签
p[ align=right] 表示选择所有align 属性为 right 的标签
firefox 可以识别,IE则不认识这种写法
Paragraph 1
Paragraph 2
Paragraph 3
Paragraph 4
[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]
属性选择器很有意思,以前发过相关日志,有4种写法:
[att] 匹配带有 att 属性的元素
[att=val] 匹配 att 属性值为 val 的元素
[att~=val] 匹配 att 属性中至少有一个 val值 的元素(用空格分开的多个值)
[att|=val] 匹配 att 属性中以 val值开头,用 - 连接的属性值的元素(用 - 连接的值)
举例如下:
[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]
更多用法变化,请参考w3c 相关说明:
http://www.w3.org/TR/REC-CSS2/selector.html#attribute-selectors
子对象选择器(child selectors)
注意,descendant selectors 和 child selectors 的区别。我个人的叫法是 包含选择器 和 子选择器,我不知道正式翻译应该是什么,计算机这东西本来就不适合用中文来表达,:(
p span 表示选择 p 内所有 span 标签
p>span 表示选择 p 内所有第一层子级 span 标签
IE认识descendant selectors(包含选择器) 的写法但不认识 child selectors(子选择器)的写法。
guoshuang testing…
guoshuang testing…
guoshuang testing…
guoshuang testing…
[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]
w3c 相关说明参考:
http://www.w3.org/TR/REC-CSS2/selector.html#child-selectors
/**/注释的方法
在选择器后立刻加上 /* */ 屏蔽低版本(5.0以下)的IE浏览器。我记得近日看过一个详细说明,应该在我的 365key 中,注释不同的写法,花样繁多的 hack,:(((
p/* */ {background:red;}
The media attribute(media 属性)
当然了,低版本的浏览器怎么能认识这个呢?
或者
CSS:
@media all {
… /* stylesheet rules here */
}
@import rule
很多低版本的浏览器,不认识前面这三个。
带引号的,不带引号的
@import url(”../hide1a.css”);
@import url(../hide1a.css);
不带url()的
@import “../hide2.css”;
IE 6也不认识这个(也就是说,屏蔽目前的所有IE)
带 media 说明的
@import url(../hide1b.css) screen;
具体在:
http://w3development.de/css/hide_css_from_browsers/import/
Tantek’s Hack
先屏蔽 netscape 4.x 以后再用这个,因为在netscape 4.x 会报错。屏蔽下列浏览器
* Netscape 4.x
* Mac IE 4.5 and lower
* Win IE 5.5 and lower
* Win Amaya 5.1
p#tantek {
voice-family: “”}”"; /* some browsers have a parsing bug */
/* and will ignore the following rules */
voice-family: inherit;
color: #f00;
}
其它 hack 方法若干:
http://w3development.de/css/hide_css_from_browsers/more/
一些调试页面的时候使用的 javascript,拷贝在地址栏粘贴,回车执行或者收到收藏夹中,方便以后调试页面时使用.
Dump Scripts
显示页面中包含的javascript脚本内容
javascript:var%20jsArray%20=%20document.getElementsByTagName(%22script%22);%20var%20theWinref;%20var%20theHTML;%20var%20scriptObj;%20var%20frameURL;%20var%20frameObj;if(jsArray.length%20%3E%200)%20%7B%20theWinref%20=%20window.open(”,’_blank’,'width=320,height=255,toolbar=no,location=no,directories=no,status=yes,menubar=no,resizeable=yes’);%20theWinref.document.write(’%3Chtml%3E%3Chead%3E%3Ctitle%3E’%20+%20window.location.href%20+%20′%3C/title%3E%3C/head%3E%3Cbody%3E’+%20jsArray.length+’%20Script(s)%20Found:’);%20theWinref.resizeTo(600,200);theWinref.moveTo(0,0);%20%7D%20for%20(var%20i%20=%200;%20i%20%3C%20jsArray.length;%20i++)%20%7B%20scriptObj%20=%20jsArray%5Bi%5D;%20if(scriptObj.hasAttribute(%22src%22))%7B%20frameURL%20=%20scriptObj.getAttribute(%22src%22);%20theWinref.document.write(%22%3Cp%20style=’background-color:%23999933;’%3EScript%20%22+eval(i+1)+%22%20via%20%22%20+%20frameURL%20+%20%22%3Cbr%3E%3Ciframe%20height=’100%25′%20width=’100%25′%20id=’url%22+%20i%20+%20%22′%20src=’%22%20+%20frameURL%20+%20%22′%3E%3C/iframe%3E%3C/p%3E%22);%20frameObj%20=%20theWinref.document.getElementById(%22url%22%20+%20i);%20%7D%20else%20%7B%20theHTML%20=%20scriptObj.innerHTML;%20theWinref.document.write(%22%3Cp%20style=’background-color:%23999933;’%3EScript%20%22+eval(i+1)+%22:%3Cbr%3E%3Cpre%3E%22%20+%20theHTML%20+%20%22%3C/pre%3E%3C/p%3E%22);%20%7D%20%7D%20if(jsArray.length%20%3E%200)%20%7B%20theWinref.document.write(’%3C/body%3E%3C/html%3E’);%20%7D
Execute Arbitrary Scripts
显示一个提示窗口,可以输入javascript代码并执行
javascript:var%20c=’document.location.href’,r=”;while(c=prompt(r+’Enter%20the%20code%20to%20be%20executed:’,c)){try{r=’Returned:%20′+eval(c)+’
‘;}catch(err){r=’Error:%20′+err.message+’
‘;}}
调试页面的时候可以使用
Show Tables
显示表格边框
javascript:var%20f=function(tt,col){var%20a=document.getElementsByTagName(tt);for(var%20b=0;b%3Ca.length;b++){a[b].style.border=’1px%20solid%20%23′+col;}};f(’table’,'00F’);f(’td’,'0F0′);f(’th’,'0FF’);
Show DIV Borders
显示 Div 的边框
javascript:var%20a=document.getElementsByTagName(’div’);for(var%20b=0;b%3Ca.length;b++){a[b].style.border=’1px%20solid%20%23F00′;}
Show DIV Borders with their IDs
显示 Div 的边框和 Id
javascript:var%20a=document.getElementsByTagName(’div’);var%20aspan;var%20txtNode;for(var%20b=0;%20b%20<%20a.length;b++){%09a[b].style.border='1px%20solid%20red';%09%09if(a[b].id%20!=%20''){%09%09aspan%20=%20document.createElement('span');%09%09aspan.setAttribute('id',%20't_ttp'%20+%20a[b].id);%09%09aspan.setAttribute('style',%20'position:relative;top:1px;left:1px;%20margin:10px%200px%200px%2010px;background-color:%20beige;max-width:220;padding:%202px%2010px%202px%2010px;border:%201px%20solid%20#C0C0C0;font:%20normal%2010px/12px%20verdana;color:%20#000;text-align:left;display:%20block;');%09%09txtNode%20=%20document.createTextNode('div:%20'%20+%20a[b].id);%09%09aspan.appendChild(txtNode);%09%09a[b].appendChild(aspan);}}
页面验证
分别为 html css 和 link 验证
javascript:void(document.location=’http://validator.w3.org/check?uri=’+document.location);
javascript:void(document.location=’http://jigsaw.w3.org/css-validator/validator?uri=’+document.location);
javascript:void(document.location=’http://validator.w3.org/checklink?url=’+document.location);
以上来自 Safari Developer FAQ
代码导致编辑器混乱,只好先另开一篇日志继续写
这是tirm空格的自定义函数
String.prototype.LTrim=new Function(”return this.replace(/^\s+/,”)”)
String.prototype.RTrim=new Function(”return this.replace(/\s+$/,”)”)
String.prototype.Trim= new Function(”return this.replace(/^\s+|\s+$/g,”)”)
真正地刷新页面
使用 location.reload() 可以刷新页面,但是是否使用cache 缓存却是服务器端配置的问题。一个客户端的技巧是,在地址栏构造一个“查询”,如当前时间
location.replace(location.href+’?d=’+new Date().valueOf())
如果地址已经是“查询”字符串,则构造
location.replace(location.href+’&d=’+new Date().valueOf())
传送后台变量到前台javascript
jsvar=”<%= aspvar %>“; // asp jsp 写法
jsvar=”” // php 写法
得到随机数
function Random(x) { return Math.floor(x*Math.random()) }
得到 0 到 x-1 之间的数字;Random(x)+1 得到 1 到 x 之间的数字。
关于 a href=”javascript:…”
不推荐这种写法,最好用
注意:这种写法会导致 动画 gif 停止播放(IE中)
[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]
不合法 ID 对象的获取
以前见过一些应用,比如动态的ID等,不一定非要用于不合法的 ID 名称,而且请尽量不要使用这些非法的命名。document.all 就没有这个好处了,比如有一次后台给出的名称含有 .
alert(document.getElementById("aa[**?]“).childNodes[0].nodeValue)
[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]
alert(document.getElementById("a.a").childNodes[0].nodeValue)
[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]
.语法(dot notation)与 [] 语法(square brackets notation)
以前见过一个文章专门讨论 [] 语法,很有意思。
test
alert(document["bo"+"dy"].innerHTML)
[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]
DOM简写的问题
common shortcut (introduced by IE and reproduced in some other browsers) in accessing DOM elements
不推荐简写,容易产生混淆。下面代码在firefox可以正确处理,IE不行,必须关掉一个 a
a=3
alert(a)
alert(document.getElementById("a"))
[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]
javascript调试问题
IE双击左下角的错误图标;mozilla类浏览器都有 javascript console(控制台)
BTW:firefox 在地址栏运行 javascript: 就可以打开控制台,我刚才就这么一弄,firefox没有响应,很慢,只好强关,哎,郁闷,一堆打开的页面还没有看呢…
cookie代码
function testCookies() {
var exp = new Date();
exp.setTime(exp.getTime() + 1800000);
// first write a test cookie
setCookie(”cookies”, “cookies”, exp, false, false, false);
if (document.cookie.indexOf(’cookies’) != -1) {
alert(”Got Cookies!”);
}
else {
alert(”No Cookies!”);
}
// now delete the test cookie
exp = new Date();
exp.setTime(exp.getTime() - 1800000);
setCookie(”cookies”, “cookies”, exp, false, false, false);
}
function setCookie(name, value, expires, path, domain, secure) {
var curCookie = name + “=” + escape(value) +
((expires) ? “; expires=” + expires.toGMTString() : “”) +
((path) ? “; path=” + path : “”) +
((domain) ? “; domain=” + domain : “”) +
((secure) ? “; secure” : “”);
document.cookie = curCookie;
}
Favicon代码
注意,favicon 现在可不仅仅是为了美化收藏夹,没有指定 favicon 的 iie 会汇报错误日志!
framename.print()在IE中不能打印需要的框架?
必须先给予该框架焦点。
frameref.focus();
frameref.print();
parseInt()的进制问题
0 开始的字符串表示八进制,0x 开头的表示十六进制,可以设定第二个参数来明确指定进制。
Octal 八进制
Hexadecimal 十六进制
alert(parseInt("011"));
alert(parseInt("9"));
alert(parseInt("0x17"));
alert(parseInt("011",10));
alert(parseInt("17",16));
[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]
字符串空格处理
guoshuang改进版
String.prototype.LTrim=function(){return this.replace(/^s+/,'')}
String.prototype.RTrim=function(){return this.replace(/s+$/,'')}
String.prototype.Trim=function(){return this.replace(/^s+|s+$/g,'')}
String.prototype.AllTrim=function(){return this.replace(/s+/g,'')}
test=" 1 2 3 4 ";
document.getElementById("test1").onfocus=function(){
document.getElementById("test1").value=test.LTrim();
document.getElementById("test1").select();
}
document.getElementById("test2").onfocus=function(){
document.getElementById("test2").value=test.RTrim();
document.getElementById("test2").select();
}
document.getElementById("test3").onfocus=function(){
document.getElementById("test3").value=test.Trim();
document.getElementById("test3").select();
}
document.getElementById("test4").onfocus=function(){
document.getElementById("test4").value=test.AllTrim();
document.getElementById("test4").select();
}
[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]
没有打开就打开一个,打开了就指定页面并设置焦点
var myWin=null;
function openWin(aURL) {
if (!myWin || myWin.closed ) {
myWin=window.open(aURL,’myWin’);
} else{
myWin.location=aURL;
myWin.focus();
}
}