`
T240178168
  • 浏览: 362292 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

Javascript总结(代码篇)

    博客分类:
  • JS
阅读更多

1)Alert box
<html>
<head>
<script type="text/javascript">
function disp_alert()
{
alert("I am an alert box!!")
}
</script>
</head>
<body>
<input type="button" onclick="disp_alert()" value="Display alert box" />
</body>
</html>
(2) confirm box
<html>
<head>
<script type="text/javascript">
function disp_confirm()
  {
  var r=confirm("Press a button")
  if (r==true)
    {
    document.write("You pressed OK!")
    }
  else
    {
    document.write("You pressed Cancel!")
    }
  }
</script>
</head>
<body>
<input type="button" onclick="disp_confirm()" value="Display a confirm box" />
</body>
</html>
(3)
Prompt box
<html>
<head>
<script type="text/javascript">
function disp_prompt()
  {
  var name=prompt("Please enter your name","Harry Potter")
  if (name!=null && name!="")
    {
    document.write("Hello " + name + "! How are you today?")
    }
  }
</script>
</head>
<body>
<input type="button" onclick="disp_prompt()" value="Display a prompt box" />
</body>
</html>
(4)
Call a function1
<html>
<head>
<script type="text/javascript">
function myfunction()
{
alert("HELLO")
}
</script>
</head>
<body>
<form>
<input type="button"
onclick="myfunction()"
value="Call function">
</form>
<p>By pressing the button, a function will be called. The function will alert a message.</p>
</body>
</html>

(5)
Function with an argument
<html>
<head>
<script type="text/javascript">
function myfunction(txt)
{
alert(txt)
}
</script>
</head>
<body>
<form>
<input type="button"
onclick="myfunction('Hello')"
value="Call function">
</form>
<p>By pressing the button, a function with an argument will be called. The function will alert
this argument.</p>
</body>
</html>

(7)Function with an argument 2
<html>
<head>
<script type="text/javascript">
function myfunction(txt)
{
alert(txt)
}
</script>
</head>
<body>
<form>
<input type="button"
onclick="myfunction('Good Morning!')"
value="In the Morning">
<input type="button"
onclick="myfunction('Good Evening!')"
value="In the Evening">
</form>
<p>
When you click on one of the buttons, a function will be called. The function will alert
the argument that is passed to it.
</p>
</body>
</html>
(8)
Function_return
<html>
<head>
<script type="text/javascript">
function myFunction()
{
return ("Hello, have a nice day!")
}
</script>
</head>
<body>
<script type="text/javascript">
document.write(myFunction())
</script>
<p>The script in the body section calls a function.</p>
<p>The function returns a text.</p>
</body>
</html>
(9)

<html>
<head>
<script type="text/javascript">
function product(a,b)
{
return a*b
}
</script>
</head>
<body>
<script type="text/javascript">
document.write(product(4,3))
</script>
<p>The script in the body section calls a function with two parameters (4 and 3).</p>
<p>The function will return the product of these two parameters.</p>
</body>
</html>
(10)
for
<html>
<body>
<script type="text/javascript">
for (i = 0; i <= 5; i++)
{
document.write("The number is " + i)
document.write("<br />")
}
</script>
<p>Explanation:</p>
<p>This for loop starts with i=0.</p>
<p>As long as <b>i</b> is less than, or equal to 5, the loop will continue to run.</p>
<p><b>i</b> will increase by 1 each time the loop runs.</p>
</body>
</html>

(11)
<html>
<body>
<script type="text/javascript">
for (i = 1; i <= 6; i++)
{
document.write("<h" + i + ">This is header " + i)
document.write("</h" + i + ">")
}
</script>
</body>
</html>

(12)
while
<html>
<body>
<script type="text/javascript">
i = 0
while (i <= 5)
{
document.write("The number is " + i)
document.write("<br />")
i++
}
</script>
<p>Explanation:</p>
<p><b>i</b> is equal to 0.</p>
<p>While <b>i</b> is less than , or equal to, 5, the loop will continue to run.</p>
<p><b>i</b> will increase by 1 each time the loop runs.</p>
</body>
</html>
(13)
do...while
<html>
<body>
<script type="text/javascript">
i = 0
do
{
document.write("The number is " + i)
document.write("<br />")
i++
}
while (i <= 5)
</script>
<p>Explanation:</p>
<p><b>i</b>  equal to 0.</p>
<p>The loop will run</p>
<p><b>i</b> will increase by 1 each time the loop runs.</p>
<p>While <b>i</b> is less than , or equal to, 5, the loop will continue to run.</p>

</body>
</html>
(14)
break
<html>
<body>
<script type="text/javascript">
var i=0
for (i=0;i<=10;i++)
{
if (i==3){break}
document.write("The number is " + i)
document.write("<br />")
}
</script>
<p>Explanation: The loop will break when i=3.</p>
</body>
</html>
(15)
try...catch
<html>
<head>
<script type="text/javascript">
var txt=""
function message()
{
try
   {
   adddlert("Welcome guest!")
   }
catch(err)
   {
   txt="There was an error on this page.\n\n"
   txt+="Error description: " + err.description + "\n\n"
   txt+="Click OK to continue.\n\n"
   alert(txt)
   }
}
</script>
</head>
<body>
<input type="button" value="View message" onclick="message()" />
</body>
</html>


分享到:
评论

相关推荐

    C#执行Javascript代码的几种方法总结

    本篇文章主要是对C#执行Javascript代码的几种方法进行了详细的总结介绍,需要的朋友可以过来参考下,希望对大家有所帮助

    前端代码规范总结(3篇).zip

    前端代码规范总结(3篇),包括HTML、CSS、JAVASCRIPT等代码规范的总结心得!

    javascript代码优化的8点总结

    本篇文章给大家分享了关于javascript代码优化的8点总结,希望我们整理的内容能够帮助到大家。

    【JavaScript源代码】JavaScript 判断浏览器是否是IE.docx

    都是支持到IE8及已上版本即可,那么这篇文章,粗浅的总结一下,如何判断浏览器是IE及版本是8.0 首先,有些属性和方法是所有版本IE都不支持,那么只需要判断是否是IE即可  以下三种是我在项目中,用到的方法,如有...

    【JavaScript源代码】Nodejs实现内网穿透服务.docx

     也许你很难从网上找到一篇从代码层面讲解内网穿透的文章,我曾搜过,未果,遂成此文。  1. 局域网内代理  我们先来回顾上篇,如何实现一个局域网内的服务代理?因为这个非常简单,所以,直接上代码。  const ...

    JavaScript代码性能优化总结篇

    下面是我总结的一些小技巧,仅供参考。 以下代码基本上在jQuery的源码里面都可以看到,如有说得不对的地方,请大家指出。 尽量使用源生方法 javaScript是解释性语言,相比编译性语言执行速度要慢。浏览器已经实现的...

    JavaScript知识点总结(十六)之Javascript闭包(Closure)代码详解

    很早就接触过闭包这个概念了,但是一直糊里糊涂的,没有能够弄明白JavaScript的闭包到底是什么,有什么用,今天在网上看到了一篇讲JavaScript闭包的文章(原文链接),讲得非常好,这下算是彻底明白了JavaScript的闭包...

    【JavaScript源代码】一篇文章弄懂js中的typeof用法.docx

    一篇文章弄懂js中的typeof用法  目录 基础 返回类型 string 和 boolean number 和 bigint symbol undefined function object 其他 常见问题 引用错误 typeof null typeof 的局限性 扩展:BigInt 类型 总结 基础 ...

    深入理解jquery和javascript系列

    深入理解JavaScript系列,超实用的jQuery代码段,jQuery源码分析系列。

    JavaScript代码性能优化总结(推荐)

    下面小编就为大家带来一篇JavaScript代码性能优化总结(推荐)。小编觉得挺不错的,现在分享给大家,也给大家做个参考,一起跟随小编过来看看吧,祝大家游戏愉快哦

    使用javascript实现页面定时跳转总结篇

    下面对使用JavaScript实现页面定时跳转(也称倒计时跳转)做一下总结,各种定时跳转代码记录如下: (1)使用 setTimeout 函数实现定时跳转(如下代码要写在body区域内) 代码如下: [removed] // 3秒钟之后跳转到...

    如何改进javascript代码的性能

    本来在那片编写可维护性代码文章后就要总结这篇代码性能文章的,耽搁了几天,本来也是决定每天都要更新一篇文章的,因为以前欠下太多东西没总结,学过的东西没去总结真的很快就忘记了,记录一下在你脑力留下更深的...

    JavaScript中常用的简洁高级技巧总结

    编程是一件很快乐的事,实现一个目的,我们可以有很多方法路径,在这篇文章我们介绍一些JavaScript的奇技淫巧,仅供大家参考,各路大神在平时的代码编写时,如很多简洁高效的书写方式; 下面话不多说了,来一起看看...

    JavaScript中判断整数的多种方法总结

    之前记录过JavaScript中判断为数字类型的多种方式,这篇看看如何判断为整数类型(Integer)。 JavaScript中不区分整数和浮点数,所有数字内部都采用64位浮点格式表示,和Java的double类型一样。但实际操作中比如数组...

    Javascript中for循环语句的几种写法总结对比

    如果您希望一遍又一遍地运行相同的代码,并且每次的值都不同,那么使用循环是很方便的,javascript中for循环也是非常常用的,下面这篇文章主要介绍了Javascript中for循环的几种写法,需要的朋友可以参考借鉴,一起来看...

    CodeMirror js代码加亮使用总结

    这几天除了上课之外有空我都是在啃着它的源码,在网上相关资料基本一点都没找到,发觉看起来真是很吃力,这篇总结也只是说个大概原理,具体细节我也很多不明白,虽然很多代码都读得懂,但是串联起来有很大问

    javascript变量作用域使用中常见错误总结

    前言:javascript里变量作用域是个经常让人头痛抓狂的问题,下面通过10++个题目,对经常遇到又容易出错的情况进行了简单总结,代码样例很短很简单 题目一 代码如下: var name = ‘casper’; alert&#40;name&#41;; /...

    JavaScript实现跑马灯抽奖活动实例代码解析与优化(二)

    在上篇文章给大家介绍了JavaScript实现跑马灯抽奖活动实例代码解析与优化(一),既然是要编写插件。那么叫做“插件”的东西肯定是具有的某些特征能够满足我们平时开发的需求或者是提高我们的开发效率。那么叫做插件...

Global site tag (gtag.js) - Google Analytics