快速认识JavaScript

熟悉JavaScript基本语法

窗口交互方法

通过DOM进行网页元素的操作

学会如何编写JS代码

运用JavaScript去操作HTML元素和CSS样式

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>热身</title>
</head>
<body>
<p id="p1">我是第一段文字</p>
<p id="p2">我是第二段文字</p> <script type="text/javascript">
document.write("hello");
document.getElementById("p1").style.color="blue";
</script>
</body>
</html>

[外链图片转存失败(img-suQFlZh3-1564302201976)(https://upload-images.jianshu.io/upload_images/11158618-67dcaa8a99832105.png?imageMogr2/auto-orient/strip|imageView2/2/w/1240)]

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb18030">
<title>插入js代码</title>
<script type="text/javascript">
document.write("开启JS之旅!");
</script>
</head>
<body>
</body>
</html>

[外链图片转存失败(img-vu9DMpGY-1564302201978)(https://upload-images.jianshu.io/upload_images/11158618-3f5db18412ba15e9.png?imageMogr2/auto-orient/strip|imageView2/2/w/1240)]

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>引用JS文件</title>
<script src="script.js"></script>
</head>
<body>
</body>
</html>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JS代码的位置</title>
<script type="text/javascript">
document.write("I love");
</script>
</head>
<body>
<script type="text/javascript">
document.write("javascript");
</script>
</body>
</html>

[外链图片转存失败(img-pFvvXb3U-1564302201980)(https://upload-images.jianshu.io/upload_images/11158618-22bea5891e5ebbd4.png?imageMogr2/auto-orient/strip|imageView2/2/w/1240)]

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>认识语句</title>
<script type="text/javascript">
document.write("Hello");
document.write("world");
</script>
</head>
<body>
</body>
</html>

[外链图片转存失败(img-96XJYpJO-1564302201980)(https://upload-images.jianshu.io/upload_images/11158618-d250649483e98538.png?imageMogr2/auto-orient/strip|imageView2/2/w/1240)]

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>隐藏的注释</title>
<script type="text/javascript">
document.write("神奇的JS,快把我们隐藏了!"); // 快快把我变成单行注释
/*知道吗
JS可以实现很多动态效果
快来学习吧!*/
</script>
</head>
<body>
</body>
</html>

[外链图片转存失败(img-8DEDHucN-1564302201982)(https://upload-images.jianshu.io/upload_images/11158618-3b8f7aa75f39bbc9.png?imageMogr2/auto-orient/strip|imageView2/2/w/1240)]

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>变量</title>
<script type="text/javascript">
var mynum = 8;
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>判断语句</title>
<script type="text/javascript">
var score =80; //score变量存储成绩,初值为80
if(score>80)
{
document.write("很棒,成绩及格了。");
}
else
{
document.write("加油,成绩不及格。");
}
</script>
</head>
<body>
</body>
</html>

[外链图片转存失败(img-Mb0XgWyN-1564302201984)(https://upload-images.jianshu.io/upload_images/11158618-348a707a1a92570f.png?imageMogr2/auto-orient/strip|imageView2/2/w/1240)]

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>函数调用</title>
<script type="text/javascript">
function contxt() //定义函数
{
alert("哈哈,调用函数了!");
}
</script>
</head>
<body>
<form>
<input type="button" value="点击我" onclick="contxt()" />
</form>
</body>
</html>

[外链图片转存失败(img-3Hi1jdZD-1564302201986)(https://upload-images.jianshu.io/upload_images/11158618-a78fd94f2fe1b1b4.png?imageMogr2/auto-orient/strip|imageView2/2/w/1240)]

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>document.write</title>
<script type="text/javascript">
var mystr="我是";
var mychar="JavaScript";
document.write(mychar+"<br>");
document.write(mystr+mychar+"的忠实粉丝!")
</script>
</head>
<body>
</body>
</html>

[外链图片转存失败(img-4gCtVAQU-1564302201988)(https://upload-images.jianshu.io/upload_images/11158618-e7114534d269d8fd.png?imageMogr2/auto-orient/strip|imageView2/2/w/1240)]

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>alert</title>
<script type="text/javascript">
function rec(){
var mychar="I love JavaScript";
alert(mychar)
}
</script>
</head>
<body>
<input name="button" type="button" onClick="rec()" value="点击我,弹出对话框" />
</body>
</html>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>alert</title>
<script type="text/javascript">
function rec(){
var mychar="I love JavaScript";
alert(mychar)
}
</script>
</head>
<body>
<input name="button" type="button" onClick="rec()" value="点击我,弹出对话框" />
</body>
</html>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>confirm</title>
<script type="text/javascript">
function rec(){
var mymessage= confirm("你是女士吗?");
if(mymessage==true)
{
document.write("你是女士!");
}
else
{
document.write("你是男士!");
}
}
</script>
</head>
<body>
<input name="button" type="button" onClick="rec()" value="点击我,弹出确认对话框" />
</body>
</html>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>prompt</title>
<script type="text/javascript">
function rec(){
var score; //score变量,用来存储用户输入的成绩值。
score = prompt("你的分数是多少?");
if(score>=90)
{
document.write("你很棒!");
}
else if(score>=75)
{
document.write("不错吆!");
}
else if(score>=60)
{
document.write("要加油!");
}
else
{
document.write("要努力了!");
}
}
</script>
</head>
<body>
<input name="button" type="button" onClick="rec()" value="点击我,对成绩做评价!" />
</body>
</html>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>window.open</title>
<script type="text/javascript">
function Wopen(){
window.open('http://www.123.com','_blank','height=600,width=400,top=100,left=0'); }
</script>
</head>
<body>
<input name="button" type="button" onClick="Wopen()" value="点击我,打开新窗口!" / >
</body>
</html>

[外链图片转存失败(img-d3h6IynX-1564302201989)(https://upload-images.jianshu.io/upload_images/11158618-632c6710eac6846d.png?imageMogr2/auto-orient/strip|imageView2/2/w/1240)]

语法:

window.open([URL], [窗口名称], [参数字符串])

_blank:在新窗口显示目标网页

_self:在当前窗口显示目标网页

_top:框架网页中在上部窗口中显示目标网页

window.close(); //关闭本窗口

<窗口对象>.close(); //关闭指定的窗口

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>close()</title>
<script type="text/javascript">
var mywin=window.open("http://www.123.com");
mywin.close();
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title> new document </title>
<meta http-equiv="Content-Type" content="text/html; charset=gbk"/>
<script type="text/javascript"> // 新窗口打开时弹出确认框,是否打开
function openWindow(){
var wep;
var op;
wep = confirm("是否打开新网页?");
if(wep==true)
{
op = prompt("默认网址为:","http://www.123.com/");
if(op!=null)
window.open(op,'_blank','width=400,height=500,menubar=no,toolbar=no');
}
else
alert("操作结束!!"); }
// 通过输入对话框,确定打开的网址,默认为 http://www.imooc.com/ //打开的窗口要求,宽400像素,高500像素,无菜单栏、无工具栏。 </script>
</head>
<body>
<input type="button" value="新窗口打开网站" onclick="openWindow()" />
</body>
</html>

HTML代码分解为DOM节点层次图:

[外链图片转存失败(img-C588ILaB-1564302201991)(https://upload-images.jianshu.io/upload_images/11158618-361ea0d15e0bddbc.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)]

通过ID获取元素

document.getElementById(“id”)

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>document.getElementById</title>
</head>
<body>
<p id="con">JavaScript</p>
<script type="text/javascript">
var mychar= ;
document.write("结果:"+mychar); //输出获取的P标签。
document.getElementById("con")
</script>
</body>
</html>

innerHTML 属性

innerHTML 属性用于获取或替换 HTML 元素的内容。

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>innerHTML</title>
</head>
<body>
<h2 id="con">javascript</H2>
<p> JavaScript是一种基于对象、事件驱动的简单脚本语言,嵌入在HTML文档中,由浏览器负责解释和执行,在网页上产生动态的显示效果并实现与用户交互功能。</p>
<script type="text/javascript">
var mychar= ;
document.write("原标题:"+mychar.innerHTML+"<br>"); //输出原h2标签内容
document.getElementById("con")
document.write("修改后的标题:"+mychar.innerHTML); //输出修改后h2标签内容
mychar.innerHTML="Hello world";
</script>
</body>
</html>

改变 HTML 样式

语法:

Object.style.property=new style;

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>style样式</title>
</head>
<body>
<h2 id="con">I love JavaScript</H2>
<p> JavaScript使网页显示动态效果并实现与用户交互功能。</p>
<script type="text/javascript">
var mychar= document.getElementById("con");
mychar.style.color="red";
mychar.style.backgroundColor ="#ccc";
mychar.style.width="300px";
</script>
</body>
</html>

显示和隐藏(display属性)

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>display</title>
<script type="text/javascript">
function hidetext()
{
var mychar = document.getElementById("con");
mychar.style.display="none";
}
function showtext()
{
var mychar = document.getElementById("con");
mychar.style.display="block";
}
</script>
</head>
<body>
<h1>JavaScript</h1>
<p id="con">做为一个Web开发师来说,如果你想提供漂亮的网页、令用户满意的上网体验,JavaScript是必不可少的工具。</p>
<form>
<input type="button" onclick="hidetext()" value="隐藏内容" />
<input type="button" onclick="showtext()" value="显示内容" />
</form>
</body>
</html>

className 属性设置或返回元素的class 属性。

获取元素的class 属性

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>className属性</title>
<style>
body{ font-size:16px;}
.one{
border:1px solid #eee;
width:230px;
height:50px;
background:#ccc;
color:red;
}
.two{
border:1px solid #ccc;
width:230px;
height:50px;
background:#9CF;
color:blue;
}
</style>
</head>
<body>
<p id="p1" > JavaScript使网页显示动态效果并实现与用户交互功能。</p>
<input type="button" value="添加样式" onclick="add()"/>
<p id="p2" class="one">JavaScript使网页显示动态效果并实现与用户交互功能。</p>
<input type="button" value="更改外观" onclick="modify()"/> <script type="text/javascript">
function add(){
var p1 = document.getElementById("p1");
p1.className = "one";
}
function modify(){
var p2 = document.getElementById("p2");
p2.className = "two";
}
</script>
</body>
</html>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" Content="text/html; charset=utf-8" />
<title>javascript</title>
<style type="text/css">
body{font-size:12px;}
#txt{
height:400px;
width:600px;
border:#333 solid 1px;
padding:5px;}
p{
line-height:18px;
text-indent:2em;}
</style>
</head>
<body>
<h2 id="con">JavaScript课程</H2>
<div id="txt">
<h5>JavaScript为网页添加动态效果并实现与用户交互的功能。</h5>
<p>1. JavaScript入门篇,让不懂JS的你,快速了解JS。</p>
<p>2. JavaScript进阶篇,让你掌握JS的基础语法、函数、数组、事件、内置对象、BOM浏览器、DOM操作。</p>
<p>3. 学完以上两门基础课后,在深入学习JavaScript的变量作用域、事件、对象、运动、cookie、正则表达式、ajax等课程。</p>
</div>
<form>
<!--当点击相应按钮,执行相应操作,为按钮添加相应事件-->
<input type="button" value="改变颜色" >
<input type="button" value="改变宽高" >
<input type="button" value="隐藏内容" >
<input type="button" value="显示内容" >
<input type="button" value="取消设置" >
</form>
<script type="text/javascript">
//定义"改变颜色"的函数
obj.style.color
obj.style.backgroundColor //定义"改变宽高"的函数
obj.style.width
obj.style.height //定义"隐藏内容"的函数
obj.style.display="none"; //定义"显示内容"的函数
obj.style.display="block"; //定义"取消设置"的函数
confirm() </script>
</body>
</html>

若本号内容有做得不到位的地方(比如:涉及版权或其他问题),请及时联系我们进行整改即可,会在第一时间进行处理。


请点赞!因为你们的赞同/鼓励是我写作的最大动力!

欢迎关注达叔小生的简书!

这是一个有质量,有态度的博客

[外链图片转存失败(img-z5uWZU0i-1564302201992)(https://upload-images.jianshu.io/upload_images/11158618-9ab0d3fef85d80ce?imageMogr2/auto-orient/strip|imageView2/2/w/1240)]

JavaScript的入门篇的更多相关文章

  1. Javascript之入门篇(一)

    上一篇学习了什么是JavaScript语言及其作用和特有的特点等,本篇将详细介绍JavaScript一些入门使用方式. 对于初学者来讲,由于JavaScript是嵌入到HTML页面里面的,首先创建一张 ...

  2. JavaScript(JS)入门篇

    <script type="text/javascript"> 表示在<script></script>之间的是文本类型(text),javas ...

  3. JavaScript之入门篇(二)

    终于学到后面的语法部分了,感觉这门语言基础部分和当初学习VB的时候感觉一样一样的,章节目录让我不禁又想到了VB课本.由于怕学过了,过段时间忘了,于是,大概总结一下. 数据类型部分 ① Typeof操作 ...

  4. JavaScript入门篇 编程练习

    编程挑战 一.定义"改变颜色"的函数 提示: obj.style.color obj.style.backgroundColor 二.定义"改变宽高"的函数 提 ...

  5. 慕课网JavaScript入门篇课程笔记

    1.js注释很重要 单行注释,在注释内容前加符号 “//”. <script type="text/javascript"> document.write(" ...

  6. 开心菜鸟系列----函数作用域(javascript入门篇)

      1 <!DOCTYPE html>   2 <html>   3 <script src="./jquery-1.7.2.js"></ ...

  7. 开心菜鸟系列----变量的解读(javascript入门篇)

                       console.info(         console.info(window['weiwu'])          console.info(window. ...

  8. JavaScript简单入门(补充篇)

    本文是对上一篇 JavaScript简单入门 的一些细节补充. 一.全局变量和局部变量 在<script>标签内定义的变量是当前页面中的全局变量.即 <script>标签可以直 ...

  9. JavaScript入门篇

    记录一下在慕课网学习JavaScript的过程. 以下内容均来自慕课网. 传送:https://www.imooc.com/code/401 为什么学习JavaScript 1. 所有主流浏览器都支持 ...

随机推荐

  1. 七牛云图床存储+Alfread工作流+使用QSHELL

    layout: post title: 七牛云图床存储+Alfread工作流+使用QSHELL 来源:http://www.cnblogs.com/cmi-sh-love/p/8901620.html ...

  2. Java之路---Day05

    2019-10-19-21:09:31 面向对象的封装性 封装性 概念:封装就是将一些细节信息隐藏起来,对于外界不可见 面向对象封装性在Java中的体现 1.方法就是一种封装 public class ...

  3. 手写MVC框架(二)-代码实现和使用示例

    --------上一篇:手写MVC框架(一)-再出发----- 背景 书接上文,之前整理了实现MVC框架需要写哪些东西.这周粗看了一下,感觉也没多少工作量,所以就计划一天时间来完成.周末的时间,哪会那 ...

  4. localStorage&sessionStorage&Cookie

    localStorage.sessionStorage.Cookie三者区别如下:

  5. 子网掩码与ip地址的关系

    1.什么是ip地址 在网络中,所有的设备都会被分配一个地址.这个地址就相当于某条路上的XX号XX房.其中[号]对应的号码是分配了整个子网的,而[房]对应的号码是分配给子网中的计算机的,这就是网络中的地 ...

  6. Celery:Next Steps

    参考文档:http://docs.celeryproject.org/en/latest/getting-started/next-steps.html#next-steps

  7. 一些常用的 Emoji 符号(可直接复制)

    表情类

  8. windows搭建测试环境

    1.安装python  安装地址: https://www.python.org/downloads/windows/ Download Windows x86-64 executable insta ...

  9. JS 对象 数组求并集,交集和差集

    一.JS数组求并集,交集和差集 需求场景 最近,自己项目中有一些数组操作,涉及到一些数学集的运算,趁着完成后总结一下. 简化问题之后,现有两数组a = [1, 2, 3],b = [2, 4, 5], ...

  10. JavaScript: 数据类型检测

    由于JavaScript是门松散类型语言,定义变量时没有类型标识信息,并且在运行期可以动态更改其类型,所以一个变量的类型在运行期是不可预测的,因此,数据类型检测在开发当中就成为一个必须要了解和掌握的知 ...