javascript技巧和方法(一)

1.创建脚本块
<script language="javascript">
脚本块
</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>
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 格式化字串
<html>
<head>
<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/>");
document.write(myVariable.sub() + "<br/>");
document.write(myVariable.sup() + "<br/>");
document.write(myVariable.toLowerCase() + "<br/>");
document.write(myVariable.toUpperCase() + "<br/>");
var firstString = "My String";
var finalString = firstString.bold().toLowerCase().fontcolor("red");
// -->
</script>
</head>
<body >
</body>
</html>
12 创建数组
<html>
<head>
<script language='javascript'>
<!--
var myArray = new Array(3);
myArray[0] = "Item 0";
myArray[1] = "Item 1";
myArray[2] = "Item 2";
for (i = 0; i < myArray.length; i++){
document.write(myArray[i] + "<br/>");
}
// -->
</script>
</head>
<body >
</body>
</html>
13.数组排序
<html>
<head>
<script language="JavaScript">
<!--
var myArray = new Array(5);
myArray[0] = "z";
myArray[1] = "c";
myArray[2] = "d";
myArray[3] = "a";
myArray[4] = "q";
document.write(myArray.sort());
// -->
</script>
</head>
<body >
</body>
</html>
14 分割字符串
<html>
<head>
<script language="JavaScript">
var myVariable = "a,b,c,d";
var stringArray = myVariable.split(",");
document.write(stringArray[0]);
document.write(stringArray[1]);
document.write(stringArray[2]);
document.write(stringArray[3]);
</script>
</head>
<body >
</body>
</html>
15 在页面加载完成后执行函数
<body onLoad=”functionName();”>
Body of the page
</body>
16 条件判断
<script>
<!--
var userChoice = window.confirm(“Choose OK or Cancel”);
var result = (userChoice == true) ? “OK” : “Cancel”;
document.write(result);
// -->
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值