`
T240178168
  • 浏览: 365578 次
  • 性别: 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>


分享到:
评论

相关推荐

    Javascript总结导图

    本篇内容将围绕"JavaScript总结导图"展开,深入探讨JavaScript的基础知识、核心概念以及高级特性。 1. **基础语法** JavaScript的基础包括变量声明(var、let、const)、数据类型(如字符串、数字、布尔值、null、...

    基于机器学习的JavaScript恶意代码检测系统的研究与实现

    【基于机器学习的JavaScript恶意代码检测系统的研究与实现】这篇论文关注的是网络安全领域的一个重要问题——JavaScript恶意代码的检测。JavaScript因为其与后台通信的能力和广泛的应用,常常被不法分子利用来进行...

    图片特效,常用经典Javascript代码

    本篇文章将详细讲解如何利用JavaScript实现图片特效,并结合"常用经典Javascript代码.doc"、"picture.html"和"test.html"中的实例进行说明。 一、图片加载与显示 在HTML中,我们通常使用`&lt;img&gt;`标签来插入图片,但...

    JavaScript语言基础知识总结(10张,神一样的总结!)

    这篇总结涵盖了JavaScript语言的基础知识,帮助初学者快速上手并深入理解这门强大的脚本语言。 首先,我们来看“JavaScript 数据类型”。JavaScript有七种数据类型:Undefined、Null、Boolean、Number、BigInt、...

    javascript代码自动补全插件

    本篇主要介绍两款JavaScript代码自动补全插件:Firebug和Firebug Autocompleter。 首先,我们要了解的是Firebug。Firebug是一款经典的Web开发调试工具,它集成在Firefox浏览器中,提供了HTML、CSS、JavaScript等多...

    js常用知识总结经典javascript知识总结,经典javascript知识总结

    这篇经典JavaScript知识总结涵盖了从基础语法到高级特性的多个方面,旨在帮助有一定基础的开发者巩固和扩展他们的JavaScript知识。 1. **创建脚本块**:在HTML文件中,使用`&lt;script&gt;`标签来插入JavaScript代码。...

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

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

    javascript入门篇—知识总结.docx

    JavaScript受到所有主流浏览器的支持,这意味着无论在哪种平台上,你的代码都能得到一致的执行。据统计,全球大部分网页都依赖JavaScript来实现丰富的用户体验,如动态效果、表单验证、动画等。对于Web开发者来说,...

    Javascript时钟

    在本篇文章中,我们将深入探讨如何利用JavaScript创建一个功能齐全的时钟,并理解其背后的原理。 首先,我们需要了解JavaScript中的Date对象。Date对象是JavaScript内建的全局对象,用于处理日期和时间。我们可以...

    引入JavaScript脚本代码到HTML文档中

    这篇博客“引入JavaScript脚本代码到HTML文档中”将探讨如何将JS代码整合到HTML页面,以便实现网页的交互功能。 首先,JavaScript的引入方式主要有三种:内联方式、内部脚本方式和外部脚本方式。 1. 内联方式:这...

    经典的javascript总结实例

    这篇经典的JavaScript总结实例将深入探讨DOM对象的操作和XML文档解析,这两部分是JavaScript在网页开发中不可或缺的部分。 首先,DOM(Document Object Model)是HTML和XML文档的结构化表示,它将网页内容转化为可...

    javascript 原生代码实现滚动条

    总结来说,JavaScript原生代码实现滚动条涉及到对DOM的深入理解,以及熟练运用事件监听、样式修改和动画处理等技巧。通过`wjscroll.js`这样的工具或源码,开发者可以创建出符合自己需求的定制化滚动条,提升网页的...

    实用工具 格式化Html格式,格式代javasctip格式,javascript代码压缩

    总结起来,这个压缩包提供了HTML格式化和JavaScript格式化及压缩的相关工具,对于网页程序员来说是非常实用的资源。通过使用这些工具,开发者可以更高效地管理和优化他们的代码,从而提高工作效率并提升项目质量。

    【JavaScript源代码】JavaScript实现移动小精灵的案例代码.docx

    总结一下,这篇文章简要介绍了如何利用JavaScript的`offset`属性获取元素的位置,并监听用户的点击事件来获取鼠标坐标。虽然案例没有完整展示元素的动态移动,但已经揭示了实现这一功能的基本思路。掌握这些基础知识...

    【JavaScript源代码】一篇文章教你JS函数继承.docx

    函数继承是JavaScript中实现对象之间属性和方法共享的一种机制,对于理解和编写复杂的JavaScript代码至关重要,同时也是面试中常被问及的话题。要理解这些继承方式,首先需要具备JavaScript原型链的基础知识。 **二...

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

    以上是关于HTML、CSS和JavaScript的代码规范总结。遵循这些最佳实践,不仅能够提升代码质量,还能促进团队之间的有效沟通,从而提高整体项目开发效率。记住,良好的代码规范是构建健壮、可持续的前端项目的基石。

    javascript功能代码

    本篇文章将通过一个具体的示例来讲解如何使用JavaScript语言结合HTML和jQuery库来实现一个简单的评论/回复功能。 #### 二、核心概念与技术要点 在深入了解示例代码之前,我们首先需要了解几个关键概念和技术点: ...

Global site tag (gtag.js) - Google Analytics