JavaScript方法和技巧大全
JavaScript方法和技巧大全 基础知识
1 创建脚本块 <script language=”JavaScript”> JavaScript code goes here </script>
2 隐藏脚本代码 <script language=”JavaScript”><!-- document.write(“Hello”); --> </script>
在不支持JavaScript的浏览器中将不执行相关代码
3 浏览器不支持的时候显示 <noscript> Hello to the non-JavaScript browser.</noscript>
4 链接外部脚本文件<script language=”JavaScript” src="/”filename.js"”></script>
5 注释脚本 // This is a comment
document.write(“Hello”); // This is a comment
/* All of this is a comment */
6 输出到浏览器 document.write(“<strong>Hello</strong>”);
7 定义变量 var myVariable = “some value”;
8 字符串相加 var myString = “String1” + “String2”;
9 字符串搜索 <script language=”JavaScript”><!-- var myVariable = “Hello there”;
var therePlace = myVariable.search(“there”); document.write(therePlace);// --> </script>
10 字符串替换 thisVar.replace(“Monday”,”Friday”);
11 格式化字串 <script language=”JavaScript”><!—
var myVariable = “Hello there”; document.write(myVariable.big() + “<br>”);
document.write(myVariable.blink() + “<br>”); document.write(myVariable.bold() + “<br>”);
document.write(myVariable.fixed() + “<br>”);document.write(myVariable.fontcolor(“red”) + “<br>”);
document.write(myVariable.fontsize(“18pt”) + “<br>”);document.write(myVariable.italics() + “<br>”);
document.write(myVariable.small() + “<br>”);document.write(myVariable.strike() + “<br>”);
相关文档:
JavaScript运行代码框代码
<script language="JavaScript" type="text/JavaScript">
//运行文本域代码
function runEx(cod1) {
cod=document.all(cod1)
var code=cod.value;
if (code!=""){
var newwin=window.open(’’,’’,’’); //打开一个窗口并赋给变量newwin。 ......
function setEnd(field) {
if (field.createTextRange) {
var r = field.createTextRange();
r.moveS ......
例:
page.html页面:
<html>
<head>
<title>This is a test</title>
</head>
<script type="text/javascript">
var count;
</script>
<body>
<iframe id="child" src="childPage.html" src="childPage"></iframe>
</body>
</html>
......
1.认识数组
数组就是某类数据的集合,数据类型可以是整型、字符串、甚至是对象
Javascript不支持多维数组,但是因为数组里面可以包含对象(数组也是一个对象),所以数组可以通过相互嵌套实现类似多维数组的功能
1.1 定义数组
声明有10个元素的数组
var a = new Array(10);
此时为a已经开辟了内存空间,包含10个元素 ......