JavaScript的事件绑定(方便不支持js的时候)
首先,比如我们使用JavaScript来加强我们的网页,但是我们要考虑到,如果用户的浏览器不支持JavaScript,或者用户disable了JavaScript的功能,那我们的网页能不能正常显示呢?例如下面的例子,
复制代码 代码如下: <a href = "#" onclick = "popUp('//www.jb51.net') ; return false;">其中popUp这个函数是自定义的,新打开一个窗口来限制URL中的网页。但是如果当客户端不支持时,那这个网页就不能正常工作了。所以我们在这样做的使用,也考虑到更多,使用如下的代码就会显得更加合适。
复制代码 代码如下:<a href = "//www.jb51.net" onclick = "popUp(this.href) ; return false;">
接着,作者以CSS为例子。在我们使用CSS的过程中,我们发现,除了我们使用了<link>把CSS文件给加载进来外,我们没有在我们的网页内容中加入任何css相关的代码,这样就能很好的把structure和style分开了,即我们的css的代码没有侵入我们的主要代码里面。这样就算客户端不知道css,但是我们的主要内容客户还是可以看到的,我们的内容结构也能在客户那里显示出来。所以JavaScript相当于behavior层,css相当于presentation层。JavaScript也能像CSS一样做到没有侵入性。下面是书上的一个例子。
复制代码 代码如下:<a href = "//www.jb51.net" onclick = "popUp(this.href) ; return false;">
上面这段代码已经能保证在客户端不支持JavaScript的情况下仍然可以正常的工作,但是上面的代码中出现了onclick这样的event handler。所以现在我们使用像CSS中的方式来完成我们所要的功能。如下:
复制代码 代码如下:<a href = "//www.jb51.net" class = "popup">
这样,我们能在这个页面加载完成的时候,执行window.onload中,来检测哪些<a>是使用了class,然后统一使用popUp的方法。如下代码
复制代码 代码如下:var links = document.getElementsByTagName("a");
for (var i=0 ; i<links.length ; i++) {
if (links[i].getAttribute("class") == "popup") {
links[i].onclick = function() {
popUp(this.getAttribute("href")); //Attention use this in this place. Because this is equals onClick = "popUp(this.href)"
//so we cann't use links[i].
return false;
}
}
}
这样就能更少地侵入我们html代码了。
最后,作者讲了我们要做到向后兼容和JavaScript的最小化。向后兼容,我们可以使用类似if(document.getElementById)来测试这个方法时候存在,存在了才能使用。JavaScript代码的最小化主要是为了减少JavaScript,这样能加快我们网页的加载。
下面我在看书的时候碰到不懂的问题,希望大虾们能帮忙解决一下。
对于<script>应该放在哪里?JavaScript DOM编程艺术中所说的,我们可以把<script>放在</body>之前,不要放在<head></head>里,这样可以加快我们加载page的速度。不是很理解。
原文:
The placement of your scripts in the markup also plays a big part in initial load times. Traditionally,
we were told to always place scripts in the <head> portion of the document, but there's a problem with
that. Scripts in the <head> block the browser's ability to download additional files (such as images or
other scripts) in parallel. In general, the HTTP specification suggests that browsers download no more
than two items at the same time per hostname. While a script is downloading, however, the browser
won't start any other downloads, even on different hostnames, so everything must wait until the script
has finished.
If you're following the progressive enhancement and unobtrusive methodologies discussed earlier
in the chapter, then moving your <script> tags shouldn't be an issue. You can make your pages load
faster simply by including all your <script> tags at the end of the document, directly before the </body>
tag. When the scripts load, your window load events will still apply your changes to the document.
您可能感兴趣的文章:
- JavaScript的事件绑定(方便不支持js的时候)
- js事件绑定--支持匿名函数解除绑定
- js_前台页面如何设置radio、checkbox、select三者的值以及绑定事件,方便后台接收数据
- 牛腩购物 8 整合用户留言 动软生成器 ,.NET在后置代码中输入JS提示语句(背景不会变白),repeater 循环的时候事件判断,repeater 隔行 奇数行,item列,隐藏区块 Panel 的使用,前台绑定代码的时候使用函数
- 使自定义事件支持多绑定 js
- 超链接标签绑定JS事件&&不加"javascript:;"导致的杯具
- 超链接标签绑定JS事件&&不加"javascript:;"导致的杯具
- 超链接标签绑定JS事件&&不加"javascript:;"导致的杯具
- 我的JavaScript回顾之路_05—0210—js和jq绑定事件的区别/事件冒泡
- 超链接标签绑定JS事件&&不加"javascript:;"导致的杯具
- javascript学习(十二):js 中为某个对象(控件)绑定事件通常可以采取两种手段
- javascript(六)js事件绑定浏览器兼容解决方案 attachEvent addEventListener
- 我的JavaScript回顾之路_04—0209—jsDom绑定点击事件/ js计算器/bootstrap框架
- 超链接标签绑定JS事件&&不加"javascript:;"导致的杯具
- js实例分析JavaScript中的事件委托和事件绑定
- JS同时绑定单双击事件
- JavaScript事件流 HTML和CSS代码支持页面的外观,JavaScript代码支持页面的行为,而JavaScript与HTML之间的交互是通过事件实现的。事件,是文档或者浏览器窗口中发生
- ajax(jquery) 刷新,重新绑定js事件
- js绑定事件、传递参数