to make a note that I love JavaScript. This article is only meant for some fun, and for us to be aware of some its short-comings.
1. The Name. JavaScript is NOT Java
We'll start with a fun jab at the name choice. While it was originally called Mocha, and then LiveScript, it was later changed to JavaScript. According to history, its similarities to the name Java was the result of a collaboration between Netscape and Sun, in exchange for Netscape bundling the Java runtime within their popular browser. It's also been noted that the name came, almost as a joke, due to the rivalry between LiveScript and Java for client-side scripting.
Nevertheless, it resulted in thousands of "JavaScript has nothing to do with Java" comments in forums across the web!
2. Null is an Object?
Consider this...
view plaincopy to clipboardprint?
console.log(typeof null); // object
console.log(typeof null); // object
This makes zero sense. If null is the absence of a value, t ......
//创建一个新的元素节点,元素名使用sTagName定义
oElementNode = document.createElementNode(sTagName);
//创建一个新的节点,节点名使用sTextValue定义
oTextNode = document.createTextNode(sTextValue);
//为元素赋一个新的属性,属性名使用sName
oAttribute = document.createAttribute(sName);
//创建一个新的注释节点,注释内容使用sData定义
oComment = document.createComment(sData);
//创建一个新的文档片段,该方法没有参数
oNewDoc = document.createDocumentFragment(); ......
<script language="JavaScript">
<!--
//图片按比例缩放
var flag=false;
function DrawImage(ImgD,iwidth,iheight){
//参数(图片,允许的宽度,允许的高度)
var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0){
flag=true;
if(image.width/image.height>= iwidth/iheight){
if(image.width>iwidth){
ImgD.width=iwidth;
ImgD.height=(image.height*iwidth)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
&nbs ......
浏览器:IE 8、FF 3.6、Chrome 4.0、Safari 4.0、Opera 10.1
仅有IE浏览器支持HTMLElement.onresize(比如body.onresize)
其它浏览器只支持window.onresize
先说IE的HTMLElement.onresize
使用前请确定你的心脏及血压正常,如果你定义了
body.onresize = function(){……}或者html.onresize = obj.fun;
你猜猜this指针是谁?不是body,不是html,不是obj,而是window!
这样清况下如果:
function fun(){
……
};
html.onresize = fun;
body.onresize = fun;
document.getElementById("id").onresize = fun;
以上代码完全可用,只是fun执行时,你根本不知道是哪个元素激发的。什么?你说event对像里会不会有激发的元素?肯定的说:没有!
好了,我们放轻松,没办法就没办法吧。大不了我就不管是谁激发的全都处理一遍。不过还是先看下面的代码:
function addresize(element){
element.onresize = function(){
……
};
};
addresize(body);
addresize(html);
addresize(#id);
你猜猜在onresize执行时的this是谁?别想了,还是window!
灵异的事情发生了,在省略号的代码中你可以使用elem ......
选择元素:document.getElementsByTagName,document.getElementsById,document.getElementsByName。
firstChild,lastChild,nextSibling,previousSibling
创建元素:document.createElement(),appendChild();
删除元素:removeChild();
实例:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
<style>
.txt{ color:Red; border:solid 1px #ff0000}
</style>
<script type="text/javascript">
function chElement()
{
var txtAry=document.getEleme ......
JavaScript 可算是世界上最流行的编程语言,它曾被 Web 开发设计师贴上噩梦的标签,虽然真正的噩梦其实是 DOM
API,这个被大量的开发与设计师随手拈来增强他们的 Web 前端的脚本语言,如今越来越被重视,虽则如此,JavaScript
仍然拥有很多让人费解的东西。
1. 它以 Java 命名,但并不是 Java
它最初叫 Mocha, 接着改名为 LiveScript,最后才确定命名为 JavaScript,根据历史记录,Java 的命名与
Netscape 和 Sun 之间的合作有关,作为交换条件,Netscape 在他们备受欢迎的浏览器中创建了 Java
运行时。值得一提的是,这个名字的出台几近一个玩笑,要知道,LiveScript 和 Java 在客户端脚本方面存在敌对关系。
不管怎么说,人们后来不得不一再澄清的一件事就是,JavaScript 和 Java 毫无关系。
2. Null 是个对象?
看看这段代码,它返回的是 object。
console.log(typeof null);//object
这实在令人费解,假如 null 表示空值,它怎么可以是对象?简单说,它是 JavaScript 最初版本的错误,这个错误甚至被微软的 JScript 直接借用。
3. NaN !== NaN
NaN,表示一个非数字的值,然而问题是,NaN不等于任何东西,甚至不等于它自己。
console.log(NaN === NaN);// ......