每当我们看到别人网页上的打开、打印、前进、另存为、后退、关闭本窗口、禁用右键等实现浏览器命令的链接,而自己苦于不能实现时,是不是感到很遗憾?是不是也想实现?如果能在网页上能实现浏览器的命令,将是多么有意思的事啊!下面我们就来看看如何用javascript代码实现浏览器菜单命令(以下代码在Windows XP下的浏览器中调试通过)。
一、【文件(F)】菜单中的命令的实现
1、〖打开〗命令的实现
[格式]:document.execCommand("open")
[说明]这跟VB等编程设计中的webbrowser控件中的命令有些相似,大家也可依此琢磨琢磨。
[举例]在<body></body>之间加入:
<a href="#" onclick=document.execCommand("open")>打开</a>
2、〖使用 记事本 编辑〗命令的实现
[格式]:location.replace("view-source:"+location)
[说明]打开记事本,在记事本中显示该网页的源代码。
[举例]在<body></body>之间加入:
<a href="#" onclick=location.replace("view-source:"+location)>使用 记事本 编辑</a>
3、〖另存为〗命令的实现
[格式]:document.execCommand("saveAs")
[说明]将该网页保存到本地盘的其它目录!
[举例]在<body></body>之间加入:
<a href="#" onclick=document.execCommand("saveAs")>另存为</a>
4、〖打印〗命令的实现
[格式]:document.execCommand("print")
[说明]当然,你必须装了打印机!
[举例]在<body></body>之间加入:
<a href="#" onclick=document.execCommand("print")>打印</a>
5、〖关闭〗命令的实现
[格式]:window.close();return false
[说明]将关闭本窗口。
[举例]在<body></body>之间加入:
<a href="#" onclick=window.close();return false)>关闭本窗口</a>
二、【编辑(E)】菜单中的命令的实现
〖全选〗命令的实现
[格式]:document.execCommand("selectAll")
[说明]将选种网页中的全部内容!
[举例]在<body></body>之间加入:
<a href="#" onclick=document.execCommand("selectAll")>全选</a>
三、【查看(V)】菜单中的命令的实现
1、〖刷新〗命令的实现
[格式]:location.reload() 或 history.go(0)
[说明]浏览器重新打开本页。
[举例]在<body></body>之间加入:
<a href="#" onclick=location.reload()>刷新</a>
或加入:<a href="#" onclick=history.go(0)>刷新</a>
2、〖源文件〗命令的实现
[格式]:location.replace("view-source:"+location)
[说明]查看该网页的源代码。
[举例]在<body></body>之间加入:
<a href="#" onclick=location.replace("view-source:"+location)>查看源文件</a>
3、〖全屏显示〗命令的实现
[格式]:window.open(document.location,"url","fullscreen")
[说明]全屏显示本页。
[举例]在<body></body>之间加入:
<a href="#" onclick=window.open(document.location,"url","fullscreen")>全屏显示</a>
四、【收藏(A)】菜单中的命令的实现
1、〖添加到收藏夹〗命令的实现
[格式]:window.external.AddFavorite('url', '“网站名”)
[说明]将本页添加到收藏夹。
[举例]在<body></body>之间加入:
<a href="javascript:window.external.AddFavorite('http://oh.jilinfarm.com', '胡明新的个人主页')">添加到收藏夹</a>
2、〖整理收藏夹〗命令的实现
[格式]:window.external.showBrowserUI("OrganizeFavorites",null)
[说明]打开整理收藏夹对话框。
[举例]在<body></body>之间加入:
<a href="#" onclick=window.external.showBrowserUI("OrganizeFavorites",null)>整理收藏夹</a>
五、【工具(T)】菜单中的命令的实现
〖internet选项〗命令的实现
[格式]:window.external.showBrowserUI("PrivacySettings",null)
[说明]打开internet选项对话框。
[举例]在<body></body>之间加入:
<a href="#" onclick=window.external.showBrowserUI("PrivacySettings",null)>internet选项</a>
六、【工具栏】中的命令的实现
1、〖前进〗命令的实现
[格式]history.go(1) 或 history.forward()
[说明]浏览器打开后一个页面。
[举例]在<body></body>之间加入:
<a href="#" onclick=history.go(1)>前进</a>
或加入:<a href="#" onclick=history.forward()>前进</a>
2、〖后退〗命令的实现
[格式]:history.go(-1) 或 history.back()
[说明]浏览器返回上一个已浏览的页面。
[举例]在<body></body>之间加入:
<a href="#" onclick=history.go(-1)>后退</a>
或加入:<a href="#" onclick=history.back()>后退</a>
3、〖刷新〗命令的实现
[格式]:document.reload() 或 history.go(0)
[说明]浏览器重新打开本页。
[举例]在<body></body>之间加入:
<a href="#" onclick=location.reload()>刷新</a>
或加入:<a href="#" onclick=history.go(0)>刷新</a>
七、其它命令的实现
〖定时关闭本窗口〗命令的实现
[格式]:settimeout(window.close(),关闭的时间)
[说明]将关闭本窗口。
[举例]在<body></body>之间加入:
<a href="#" onclick=settimeout(window.close(),3000)>3秒关闭本窗口</a>
如果大家还整理出其他用javascript实现的命令,不妨投稿来和大家分享。
【附】为了方便读者,下面将列出所有实例代码,你可以把它们放到一个html文件中,然后预览效果。
<a href="#" onclick=document.execCommand("open")>打开</a><br>
<a href="#" onclick=location.replace("view-source:"+location)>使用 记事本 编辑</a><br>
<a href="#" onclick=document.execCommand("saveAs")>另存为</a><br>
<a href="#" onclick=document.execCommand("print")>打印</a><br>
<a href="#" onclick=window.close();return false)>关闭本窗口</a><br>
<a href="#" onclick=document.execCommand("selectAll")>全选</a><br>
<a&nb
sp;href="#" onclick=location.reload()>刷新</a> <a href="#" onclick=history.go(0)>刷新</a><br>
<a href="#" onclick=location.replace("view-source:"+location)>查看源文件</a> <br>
<a href="#" onclick=window.open(document.location,"url","fullscreen")>全屏显示</a> <br>
<a href="javascript:window.external.AddFavorite('http://www.dansion.com', '天极网页陶吧')">添加到收藏夹</a> <br>
<a href="#" onclick=window.external.showBrowserUI("OrganizeFavorites",null)>整理收藏夹</a> <br>
<a href="#" onclick=window.external.showBrowserUI("PrivacySettings",null)>internet选项</a> <br>
<a href="#" onclick=history.go(1)>前进1</a> <a href="#" onclick=history.forward()>前进2</a><br>
<a href="#" onclick=history.go(-1)>后退1</a> <a href="#" onclick=history.back()>后退2</a><br>
<a href="#" onclick=settimeout(window.close(),3000)>3秒关闭本窗口</a><br>
以上的还是比较实用的.
Archive for the ‘xhtml+css’ Category
IE菜单控制
星期一, 三月 19th, 2007Expression在css中最简单的应用(隔行换色)
星期四, 三月 15th, 2007IE5及其以后版本支持在CSS中使用expression,用来把CSS属性和javascript表达式关联起来,这里的CSS属性可以是元素固有的属性,也可以是自定义属性。就是说CSS属性后面可以是一段javascript表达式,CSS属性的值等于javascript表达式计算的结果。
在表达式中可以直接引用元素自身的属性和方法,也可以使用其他浏览器对象。
例:简单实现隔行换色。
tr {
background-color: expression(this.sectionRowIndex==0?"#FF0000":((this.sectionRowIndex%2==0) ? "#F7F7F7" : "#FFFFFF"));
}
#FF0000是设定表格第一行背景颜色。
#F7F7F7与#FFFFFF是后续几行隔行换色。
常用CSS缩写语法总结
星期二, 三月 13th, 2007使用缩写可以帮助减少你CSS文件的大小,更加容易阅读。css缩写的主要规则如下:
颜色
16进制的色彩值,如果每两位的值相同,可以缩写一半,例如:
#000000可以缩写为#000;#336699可以缩写为#369;
盒尺寸
通常有下面四种书写方法:
property:value1; 表示所有边都是一个值value1;
property:value1 value2; 表示top和bottom的值是value1,right和left的值是value2
property:value1 value2 value3; 表示top的值是value1,right和left的值是value2,bottom的值是value3
property:value1 value2 value3 value4; 四个值依次表示top,right,bottom,left
方便的记忆方法是顺时针,上右下左。具体应用在margin和padding的例子如下:
margin:1em 0 2em 0.5em;
边框(border)
边框的属性如下:
border-width:1px;
border-style:solid;
border-color:#000;
可以缩写为一句:border:1px solid #000;
语法是border:width style color;
背景(Backgrounds)
背景的属性如下:
background-color:#f00;
background-image:url(background.gif);
background-repeat:no-repeat;
background-attachment:fixed;
background-position:0 0;
可以缩写为一句:background:#f00 url(background.gif) no-repeat fixed 0 0;
语法是background:color image repeat attachment position;
你可以省略其中一个或多个属性值,如果省略,该属性值将用浏览器默认值,默认值为:
color: transparent
image: none
repeat: repeat
attachment: scroll
position: 0% 0%
字体(fonts)
字体的属性如下:
font-style:italic;
font-variant:small-caps;
font-weight:bold;
font-size:1em;
line-height:140%;
font-family:"Lucida Grande",sans-serif;
可以缩写为一句:font:italic small-caps bold 1em/140% "Lucida Grande",sans-serif;
注意,如果你缩写字体定义,至少要定义font-size和font-family两个值。
列表(lists)
取消默认的圆点和序号可以这样写list-style:none;,
list的属性如下:
list-style-type:square;
list-style-position:inside;
list-style-image:url(image.gif);
可以缩写为一句:list-style:square inside url(image.gif);
CSS基本技巧
星期一, 三月 5th, 2007一.使用css缩写
使用缩写可以帮助减少你CSS文件的大小,更加容易阅读。
常用CSS缩写语法总结
二.明确定义单位,除非值为0
忘记定义尺寸的单位是CSS新手普遍的错误。在HTML中你可以只写width=100,但是在CSS中,你必须给一个准确的单位,比如:width: 100px width:100em。只有两个例外情况可以不定义单位:行高和0值。除此以外,其他值都必须紧跟单位,注意,不要在数值和单位之间加空格。
三.区分大小写
当在XHTML中使用CSS,CSS里定义的元素名称是区分大小写的。为了避免这种错误,我建议所有的定义名称都采用小写。
class和id的值在HTML和XHTML中也是区分大小写的,如果你一定要大小写混合写,请仔细确认你在CSS的定义和XHTML里的标签是一致的。
四.取消class和id前的元素限定
当你写给一个元素定义class或者id,你可以省略前面的元素限定,因为ID在一个页面里是唯一的,鴆las s可以在页面中多次使用。你限定某个元素毫无意义。例如:
div#content { /* declarations */ }
fieldset.details { /* declarations */ }
可以写成
#content { /* declarations */ }
.details { /* declarations */ }
这样可以节省一些字节。
五.默认值
通常padding的默认值为0,background-color的默认值是transparent。但是在不同的浏览器默认值可能不同。如果怕有冲突,可以在样式表一开始就先定义所有元素的margin和padding值都为0,象这样:
* {
margin:0;
padding:0;
}
六.不需要重复定义可继承的值
CSS中,子元素自动继承父元素的属性值,象颜色、字体等,已经在父元素中定义过的,在子元素中可以直接继承,不需要重复定义。但是要注意,浏览器可能用一些默认值覆盖你的定义。
七.最近优先原则
如果对同一个元素的定义有多种,以最接近(最小一级)的定义为最优先,例如有这么一段代码
Update: Lorem ipsum dolor set
在CSS文件中,你已经定义了元素p,又定义了一个classupdate
p {
margin:1em 0;
font-size:1em;
color:#333;
}
.update {
font-weight:bold;
color:#600;
}
这两个定义中,class=update将被使用,因为class比p更近。你可以查阅W3C的《 Calculating a selector’s specificity》 了解更多。
八.多重class定义
一个标签可以同时定义多个class。例如:我们先定义两个样式,第一个样式背景为#666;第二个样式有10 px的边框。
.one{width:200px;background:#666;}
.two{border:10px solid #F00;}
在页面代码中,我们可以这样调用
<div class=one two></div>
这样最终的显示效果是这个div既有#666的背景,也有10px的边框。是的,这样做是可以的,你可以尝试一下。
九.使用子选择器(descendant selectors)
CSS初学者不知道使用子选择器是影响他们效率的原因之一。子选择器可以帮助你节约大量的class定义。我们来看下面这段代码:
<div id=subnav>
<ul>
<li class=subnavitem> <a href=# class=subnavitem>Item 1</a></li>>
<li class=subnavitemselected> <a href=# class=subnavitemselected> Item 1</a> </li>
<li class=subnavitem> <a href=# class=subnavitem> Item 1</a> </li>
</ul>
</div>
这段代码的CSS定义是:
div#subnav ul { /* Some styling */ }
div#subnav ul li.subnavitem { /* Some styling */ }
div#subnav ul li.subnavitem a.subnavitem { /* Some styling */ }
div#subnav ul li.subnavitemselected { /* Some styling */ }
div#subnav ul li.subnavitemselected a.subnavitemselected { /* Some styling */ }
你可以用下面的方法替代上面的代码
<ul id=subnav>
<li> <a href=#> Item 1</a> </li>
<li class=sel> <a href=#> Item 1</a> </li>
<li> <a href=#> Item 1</a> </li>
</ul>
样式定义是:
#subnav { /* Some styling */ }
#subnav li { /* Some styling */ }
#subnav a { /* Some styling */ }
#subnav .sel { /* Some styling */ }
#subnav .sel a { /* Some styling */ }
用子选择器可以使你的代码和CSS更加简洁、更加容易阅读。
十.不需要给背景图片路径加引号
为了节省字节,我建议不要给背景图片路径加引号,因为引号不是必须的。例如:
background:url(images/***.gif) #333;
可以写为
background:url(images/***.gif) #333;
如果你加了引号,反而会引起一些浏览器的错误。
十一.组选择器(Group selectors)
当一些元素类型、class或者id都有共同的一些属性,你就可以使用组选择器来避免多次的重复定义。这可以节省不少字节。
例如:定义所有标题的字体、颜色和margin,你可以这样写:
h1,h2,h3,h4,h5,h6 {
font-family:Lucida Grande,Lucida,Arial,Helvetica,sans-serif;
color:#333;
margin:1em 0;
}
如果在使用时,有个别元素需要定义独立样式,你可以再加上新的定义,可以覆盖老的定义,例如:
h1 { font-size:2em; }
h2 { font-size:1.6em; }
十二.用正确的顺序指定链接的样式
当你用CSS来定义链接的多个状态样式时,要注意它们书写的顺序,正确的顺序是::link :visited :hover :active。抽取第一个字母是LVHA,你可以记忆成LoVe HAte(喜欢讨厌)。为什么这么定义,可以参考Eric Meyer的《Link Specificity》。
如果你的用户需要用键盘来控制,需要知道当前链接的焦点,你还可以定义:focus属性。:focus属性的效果也取决与你书写的位置,如果你希望聚焦元素显示:hover效果,你就把:focus写在:hover前面;如果你希望聚焦效果替代:hover效果,你就把:focus放在:hover后面。
十三.清除浮动
一个非常常见的CSS问题,定位使用浮动的时候,下面的层被浮动的层所覆盖,或者层里嵌套的子层超出了外层的范围。
通常的解决办法是在浮动层后面添加一个额外元素,例如一个div或者一个br,并且定义它的样式为clear: both。这个办法有一点牵强,幸运的是还有一个好办法可以解决,参看这篇文章《How To Clear Floats Without Structural Markup》
上面2种方法可以很
网页HTML默认的CSS样式表属性总结
星期一, 三月 5th, 2007除了inline和block的定义,主要是要注意 body|h1~h6|blockquote|menu|ul|ol|dd等标签的默认样式(margin和font-size)。
不过不同浏览器的默认值是不一样的.
html, address,
blockquote,
body, dd, div,
dl, dt, fieldset, form,
frame, frameset,
h1, h2, h3, h4,
h5, h6, noframes,
ol, p, ul, center,
dir, hr, menu, pre { display: block }
li { display: list-item }
head { display: none }
table { display: table }
tr { display: table-row }
thead { display: table-header-group }
tbody { display: table-row-group }
tfoot { display: table-footer-group }
col { display: table-column }
colgroup { display: table-column-group }
td, th { display: table-cell; }
caption { display: table-caption }
th { font-weight: bolder; text-align: center }
caption { text-align: center }
body { margin: 8px; line-height: 1.12 }
h1 { font-size: 2em; margin: .67em 0 }
h2 { font-size: 1.5em; margin: .75em 0 }
h3 { font-size: 1.17em; margin: .83em 0 }
h4, p,
blockquote, ul,
fieldset, form,
ol, dl, dir,
menu { margin: 1.12em 0 }
h5 { font-size: .83em; margin: 1.5em 0 }
h6 { font-size: .75em; margin: 1.67em 0 }
h1, h2, h3, h4,
h5, h6, b,
strong { font-weight: bolder }
blockquote { margin-left: 40px; margin-right: 40px }
i, cite, em,
var, address { font-style: italic }
pre, tt, code,
kbd, samp { font-family: monospace }
pre { white-space: pre }
button, textarea,
input, object,
select { display:inline-block; }
big { font-size: 1.17em }
small, sub, sup { font-size: .83em }
sub { vertical-align: sub }
sup { vertical-align: super }
table { border-spacing: 2px; }
thead, tbody,
tfoot { vertical-align: middle }
td, th { vertical-align: inherit }
s, strike, del { text-decoration: line-through }
hr { border: 1px inset }
ol, ul, dir,
menu, dd { margin-left: 40px }
ol { list-style-type: decimal }
ol ul, ul ol,
ul ul, ol ol { margin-top: 0; margin-bottom: 0 }
u, ins { text-decoration: underline }
br:before { content: "A" }
:before, :after { white-space: pre-line }
center { text-align: center }
abbr, acronym { font-variant: small-caps; letter-spacing: 0.1em }
:link, :visited { text-decoration: underline }
:focus { outline: thin dotted invert }
/* Begin bidirectionality settings (do not change) */
BDO[DIR="ltr"] { direction: ltr; unicode-bidi: bidi-override }
BDO[DIR="rtl"] { direction: rtl; unicode-bidi: bidi-override }
*[DIR="ltr"] { direction: ltr; unicode-bidi: embed }
*[DIR="rtl"] { direction: rtl; unicode-bidi: embed }
@media print {
h1 { page-break-before: always }
h1, h2, h3,
h4, h5, h6 { page-break-after: avoid }
ul, ol, dl { page-break-before: avoid }
ie6.0对css支持不足例举
星期四, 一月 11th, 2007在用IE中用CSS布局会出现各种问题.
本人习惯把body当然最外层的div来用.然后所有的div都float:left;进行布局,结要在此时给body设背影图片时,会出现无法解释的定位问题.特别是需要制作左边固定宽度,右边是随窗口变化的那种页面.
第二种情况,在设置:body{width:750px;margin:0px auto}这种方式合页面居中后.
<div class="layout">
<div class="left"></div>
<div class="right"></div>
</div>
设置样式表:
.layout{padding-left:200px;}
.left{margin-left:-200px;position:absolute;float:left;}
.right{}
这样设置方式可实现虽然意义不大.但IE在这种情况会一个问题.
在窗口变化后.left的div的定位总有所有不对,好像是针对body定位一样.
第三种
当把ul也当做div用,也就是ul也成了布局元素(本人习惯想少写一些代码,希望文件小点,这种想法虽然有些变态,但也是一种尝试).在IE会出会现一个很有意的问题,就是ul里的最后一个li的几个文字会重复出现在ul外面.却不影响后续布局.这种情况会一递传.在FF中却没有种情况.