本文主要讲解了关于一些前端的基础知识,用来前端的扫盲,能够对于前端有一个大致的认识,不至于对前端如何运行,代码都一窍不通。下面是本文的主要讲解方向:
这里我们从0开始,一步一步了解前端的基础知识。
形如:
<body>hello</body>
<body id="myId">hello</body>
<html>
<head>
<title>第一个页面</title>
</head>
<body>
hello world
</body>
</html>
h1-h6 有六个, 从 h1 - h6. 数字越大, 则字体越小.
<h1>代表的是标题标签,后面数字越大,最后在界面上显示的字体大小就越小,呈反比。
<h1>hello</h1>
<h2>hello</h2>
<h3>hello</h3>
<h4>hello</h4>
<h5>hello</h5>
<h6>hello</h6>
把一段比较长的文本粘贴到 html 中, 会发现并没有分成段落. 在html中使用标签括起一个段落进行换行。当然也 可以在段落内使用<br/>标签进行换行操作。
例如以下文本:
<p>段落,
在html中一般的回车并不起作用,会被解释成为一个空格<br/>但是br不一样,br标签的作用就是换行。
</p>
效果如下:
<p><b>比如b标签就是加粗</b></p>
<p><i>比如i标签就是斜体</i></p>
<p><s>比如s标签就是删除线</s></p>
<p><u>比如u就是下划线</u></p>
效果如下:
img 标签必须带有 src 属性. 表示图片的路径.
因为当前页面是在我的桌面文件里面的,而我采用的图片也是桌面上的,因此在同一路径下,直接./加图片名就可以加载了。
这里我们根据自己学到的知识,自己简单编写一个前端界面:
<html>
<head>
<meta charset="utf-8">
<title>学习页面</title>
</head>
<body>
<h1>Hello World</h1>
<h2>Hello World</h2>
<img src='./1.jpg' width="300px">
<a href="https://www.baidu.com">
这是一个链接,可以跳转到百度
</a>
</body>
</html>
点开链接之后,就直接从当前页面跳转到百度,因为没有设置target打开方式,默认就是从当前页面打开!
表单是让用户输入信息的重要途径. 分成两个部分:
<input type="text" placeholder="input标签默认是文本框"> <br/>
<input type="password" placeholder="type属性为password是密码框"> <br/>
<input type="radio" name="sex">type属性为radio是单选框,name属性相同则默认为同一组-男 <br/>
<input type="radio" name="sex" checked="checked">type属性为radio是单选框-女<br/>
<input type="checkbox"> checkbox是复选框-吃饭 <br/>
<input type="checkbox"> checkbox是复选框-睡觉 <br/>
<input type="checkbox"> checkbox是复选框-打游戏<br/>
<input type="checkbox" id="testid">
<label for="testid">label标签for属性与对应的输入框id对应起来,这时候点击文字也能选中</label><br/>
<input type="button" value="button是普通按钮" onclick="alert('alert是提示框调用函数')"><br/>
<input type="submit" value="submit是提交按钮">点击这里就会向服务器提交表单域中的表单数据<br/>
<input type="file" value="file是文件选择按钮框"><br/>
<input type="reset" value="reset是清空按钮,会清空表单域的所有数据"><br>
<html>
<head>
<meta charset="utf-8">
<title>学习页面</title>
</head>
<body>
<h1>Hello World</h1>
<h2>Hello World</h2>
<img src='./1.jpg' width="300px">
<a href="https://www.baidu.com">
这是一个链接,可以跳转到百度
</a>
<form action="http://123.249.125.60:8085/login"
method="post">
<input type="text" name="uesrname">
<input type="password" name="password">
<input type="submit" name="submit" value="提交">
</form>
</body>
</html>
我们通过这个模块,就可以将前端输入的数据传送到指定的服务器上了!
这样body就可以提取我们在前端界面输入的信息了!
CSS 能够对网页中元素位置的排版进行像素级精确控制, 实现美化页面的效果. 能够做到页面的样式和结构分离。
CSS 就是 "东方四大邪术" 之化妆术.
选 择 器 + { 一 条 / N 条 声 明 }
<html>
<head>
<meta charset="utf-8">
<title>学习页面</title>
<style>
h1 {
color: red;
font-size: 20px;
}
</style>
</head>
<body>
<h1>Hello World</h1>
<h2>Hello World</h2>
<img src='./1.jpg' width="300px">
<a href="https://www.baidu.com">
这是一个链接,可以跳转到百度
</a>
<form action="http://123.249.125.60:8085/login"
method="post">
<input type="text" name="uesrname">
<input type="password" name="password">
<input type="submit" name="submit" value="提交">
</form>
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<title>学习页面</title>
<style>
h1 {
color: red;
font-size: 20px;
}
</style>
</head>
<body>
<h1>Hello World</h1>
<h2 id="h2_title">Hello World</h2>
<img src='./1.jpg' width="300px">
<a href="https://www.baidu.com">
这是一个链接,可以跳转到百度
</a>
<form action="http://123.249.125.60:8085/login"
method="post">
<input type="text" name="uesrname">
<input type="password" name="password">
<input type="submit" name="submit" value="提交">
</form>
<button onclick="test()"> 普通的button </button>
</body>
<script>
function test()
{
//alert("你好!!!");
var h2 = document.getElementById("h2_title");
alert(h2.innerHTML);
h2.innerHTML = "Hello Kehan!"
}
</script>
</html>
点击确定按钮之后,就会更改我们所设定的内容。
我们通过输入输入框的数据,并点击确认按钮,来获取输入框里面的内容。
<html>
<head>
<meta charset="utf-8">
<title>学习页面</title>
<style>
h1 {
color: red;
font-size: 20px;
}
</style>
</head>
<body>
<h1>Hello World</h1>
<h2 id="h2_title">Hello World</h2>
<img src='./1.jpg' width="300px">
<a href="https://www.baidu.com">
这是一个链接,可以跳转到百度
</a>
<form action="http://123.249.125.60:8085/login"
method="post">
<input type="text" id="uesrname" name="uesrname">
<input type="password" name="password">
<input type="submit" name="submit" value="提交">
</form>
<button onclick="test()"> 普通的button </button>
</body>
<script>
function test()
{
//alert("你好!!!");
//var h2 = document.getElementById("h2_title");
//alert(h2.innerHTML);
//h2.innerHTML = "Hello Kehan!"
var input = document.getElementById("uesrname");
alert(input.value);
input.value = "";
}
</script>
</html>
注意我们这里的Ajax的使用使用的是jequery的Ajax,这一版本的Ajax更方便使用。
这里我们使用五子棋注册页面为例。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>注册</title>
<link rel="stylesheet" href="./css/common.css">
<link rel="stylesheet" href="./css/login.css">
</head>
<body>
<div class="nav">
网络五子棋对战游戏
</div>
<div class="login-container">
<!-- 登录界面的对话框 -->
<div class="login-dialog">
<!-- 提示信息 -->
<h3>注册</h3>
<!-- 这个表示一行 -->
<div class="row">
<span>用户名</span>
<input type="text" id="user_name" name="username">
</div>
<!-- 这是另一行 -->
<div class="row">
<span>密码</span>
<input type="password" id="password" name="password">
</div>
<!-- 提交按钮 -->
<div class="row">
<!-- 1. 给按钮添加点击事件,调用注册函数 -->
<button id="submit" onclick="reg()">提交</button>
</div>
</div>
</div>
<script src="js/jquery.min.js"></script>
<script>
//1. 给按钮添加点击事件,调用注册函数
//2. 封装实现注册函数
function reg() {
// 1. 获取两个输入框空间中的数据,组织成为一个json串
var reg_info = {
username: document.getElementById("user_name").value,
password: document.getElementById("password").value
};
console.log(JSON.stringify(reg_info));
// 2. 通过ajax向后台发送用户注册请求
$.ajax({
url : "/reg",
type : "post",
data : JSON.stringify(reg_info),
success : function(res) {
if (res.result == false) {
// 4. 如果请求失败,则清空两个输入框内容,并提示错误原因
document.getElementById("user_name").value = "";
document.getElementById("password").value = "";
alert(res.reason);
}else {
// 3. 如果请求成功,则跳转到登录页面
alert(res.reason);
window.location.assign("/login.html");
}
},
error : function(xhr) {
document.getElementById("user_name").value = "";
document.getElementById("password").value = "";
alert(JSON.stringify(xhr));
}
})
}
</script>
</body>
</html>
注册用户成功,并成功跳转到登录界面。
简单使用用例:
在index.html中创建一个websocket通信,从输入框中获取数据发送给服务器,得到响应后,展示在页面中
<html>
<head>
<meta charset="utf-8">
<title>学习页面</title>
<style>
h1 {
color: red;
font-size: 20px;
}
</style>
</head>
<body>
<h1>Hello World</h1>
<h2 id="h2_title">Hello World</h2>
<img src='./1.jpg' width="300px">
<a href="https://www.baidu.com">
这是一个链接,可以跳转到百度
</a>
<form action="http://123.249.125.60:8085/login"
method="post">
<input type="text" id="uesrname" name="uesrname">
<input type="password" name="password">
<input type="submit" name="submit" value="提交">
</form>
<button onclick="test()"> 普通的button </button>
</body>
<script>
var ws = new WebSocket("ws://123.249.125.60:8085/ws");
ws.onopen = function()
{
alert("ws 握手成功");
}
ws.onerror = function()
{
alert("ws 通信错误");
}
ws.onclose = function()
{
alert("ws 链接断开");
}
ws.onmessage = function(evt)
{
//alert(evt.data);
var h2 = document.getElementById("h2_title");
h2.innerHTML = evt.data;
}
function test()
{
ws.send(document.getElementById("uesrname").value);
document.getElementById("uesrname").value = "";
//alert("你好!!!");
//var h2 = document.getElementById("h2_title");
//alert(h2.innerHTML);
//h2.innerHTML = "Hello Kehan!"
/*
var input = document.getElementById("uesrname");
alert(input.value);
input.value = "";
*/
}
</script>
</html>
综上所述,Ajax和WebSocket各有优劣,开发者在实际开发中应根据具体的应用需求来选择合适的技术以实现最佳的用户体验和系统性能。