入门js

作者: 矮萌杰 | 来源:发表于2018-05-27 18:12 被阅读0次

javascript简称js;
在html中可进行交互,完成一些动态的功能,比如轮播图,注册表单的校验,定时弹出广告等等。
简单介绍一下javascript(以下简称js)
1.js的插入(类似于css的插入)
①写在Html的文件中

<script type="text/javascript">// type="text/javascript"为默认值,可不写
</script>

②用新的文件来写

<script type="text/javascript" src="x.js">

ps:js的代码可放在<head></head> <body></body>中

2.js的变量。
js语言为弱变量。
并且不能使用js的关键字和保留字定义变量。
定义变量:

var a;

定义变量要区分大小写,以下是两个不同变量。

var char;
var Char;

3.函数
js最常用的就是调用函数去完成某项功能。
定义函数的关键字:function.

function xx(){
    alert("警告内容");//alert为window对象,后面介绍
 }

4.js输出内容
js输出都用"document.write"进行js的内容输出。
①将要输入的内容用双引号("")括起来,直接进行输出

document.write("直接输出法");

②通过变量进行输出

var a = "通过变量输出"
document.write(a);

③可用"+"号连接要输出的两个内容

var a = " i love"
document.write(a+"中国");

④输出html中的标签起作用

var a ="i like"
document.write(a+"<br>"+"i love");//进行换行,标签也要用双引号括起来

⑤输出空格
1.&nbsp表示一个空格
2.white-space:pre

document.write("<span style='white-space:pre'>"+"1   2   3"+</span>);
//两侧必须用<span></span>包装,white-space要用单引号(')括起来。

5.几个常用window 对象(弹窗)都以<input>按钮举例
Window 对象表示浏览器中打开的窗口。
①警告窗口
alert("警告内容");
出现两个alert函数时,按照顺序依次弹出窗口。
js代码:

function xx(){
 alert("弹窗警告的内容"); 
 }

htmlu部分:

<input type="button" value="点击我就弹窗" onclick="xx()">
//onclick="xx()" 绑定函数 onclick为点击触发,赋值的函数要和js的一致。
警告示例.png

②确认窗口
confirm("提问的内容"),返回值为boolean.
常常与if连用
js:

function xx(){
  var a = confirm("你喜欢吗?")
  if(a==ture)
{
  document.write("yes");
 }
else
{
  document.write("no");
}
  }

③提问消息框
prompt();

function rec(){
var  score;
score=prompt();
if(score>=90)
{
document.write("优秀");
}
else if (score>=80)
{
document.write("良好");
}
else if (score>=70)
{
document.write("一般");
}
else if (score>=80)
{
document.write("中等");
}
}

④打开新窗口
window.open

function wopen(){
window.open([URL], [窗口名称], [参数字符串]);
//eg:window.open('http://www.imooc.com','lajiwanyi','_blank',
//'width=300,height=200,menubar=yes,toolbar=yes, 
//status=yes,scrollbars=yes')
 }
新窗口对应的参数属性值表.png

在运用window对象的时候一般都要进行绑定函数
function xx(): function 后面的"xx()"要与html中需要绑定的一致
不同的事件需要不同的东西,比如onclick,init等等。

相关文章

网友评论

    本文标题:入门js

    本文链接:https://www.haomeiwen.com/subject/bdobjftx.html