1.1 javascript插入位置
<script type="text/javascript">
...
</script>
2.1 变量
var name = "gaoqian";
console.log(name);
alert(name);
confirm(“是否退出”);
document.write(name);
2.1.2字符串
var name = "gaoqian";
var name = string("gaoqian");
var v1 = name.length;
var v2 = name[0];
var v3 = name.trim();
var v4 = name.substring(0,2);
2.1.3 数组
var v1 = [11,22,33];
var v2 = Array([11,22,33]);
v1[0] = "高倩";
v1.push("liantong");
v1.unshift("liantong");
v1.splice(索引位置,0,元素);
v1.splice(1,0,"中国");
v1.pop();
v1.shift();
v1.splice(索引位置,1);
v1.splice(2,1);
for(var idx in v1){
}
2.1.4 字典
info = {
"name":"gaoqian",
"age":18
}
info = {
name:"gaoqian",
age:18
}
info.age;
info.name = "gaozhi";
info["age"];
info["name"] = "gaozhi";
delete info["name"];
for(var key in info){
}
2.2 实例
<span id="txt">...</span>
var tag = document.getElementByld("txt");
var tag = document.creatElement("li");
tag.innerText();
tag.innerText = "...";
<body>
<table>
<thead>
<tr>
</tr>
</thead>
<tbody id="body">
</tbody>
</table>
<script type="text/javascript">
var info = {id:1, name:"gaozhi" ,age:19};
var tr = document.createElement("tr");
for(var key in info){
var text = info[key];
var td = document.creatElement("td");
td.innerText = text;
tr.appendChild(td);
}
var bodyTag = document.getElementById("body")
bodyTag.appendChild(tr);
<input type="text" placeholder="请输入内容" id="txtUser" />
<input type="button" value="点击添加" onclick="addClickInfo()">
<ul id="city">
</ul>
<script type="text/javascript">
function addClickInfo(){
var txtTag =document.getElementById("txtUser");
var newString = txtTag.value;
if(newString.length > 0){
var newTag = document.creatElement("li");
newTag.innerText = newString;
var parentTag = getElementById("city");
parentTag.appendChild(newTag);
txtTag.value = "";
}else{
alert(""输入不能为空);
}
}
3.1 jQuery引入
<script src="static/jquery-3.6.0.min.js"></script>
<script>
</script>
3.1.1 直接寻找
<h1 id="txt">zhongguo</h1>
<h1>zhongguo</h1>
$("#txt");
<h1 class="c1">zhongguo</h1>
<h1 class="c2">zhongguo</h1>
$(".c1");
<h1 class="c1">zhongguo</h1>
<div class="c2">zhongguo</div>
$("h1");
<input type='text' name="n1" />
<input type='text' name="n1" />
<input type='text' name="n2" />
$("input[name='n1']");
3.1.2 间接寻找
<div>
<div>北京</div>
<div id="c1">shanghai</div>
<div>shenzhen</div>
</div>
$("#c1").prev()
$("#c1").next()
$("#c1").parent()
$("#c1").children()
$("#c1").children(".p10")
$("#c1").siblings()
3.2 值的操作
$("#c1").text()
$("#c1").text("休息")
<input type="text" id="c2" />
$("#c2").val()
$("#c2").val("hhhh")
3.3 事件的操作
<input type="button" value="提交" onclick="getInfo()" />
<script>
function getInfo(){
}
</script>
<ul>
<li>baidu</li>
<li>google</li>
</ul>
<script>
$("li").click(function(){
})
</script>
3.4 实例
<body>
<table>
<thead>
<tr>
<td>ID</td>
<td>姓名</td>
<td>操作</td>
</tr>
</thead>
<tbody id="body">
<tr>
<td>1</td>
<td>周杰伦</td>
<td>
<input type="button" value="删除" class="delete" />
</td>
</tr>
<tr>
<td>2</td>
<td>周杰伦</td>
<td>
<input type="button" value="删除" class="delete" />
</td>
</tr>
<tr>
<td>1</td>
<td>周杰伦</td>
<td>
<input type="button" value="删除" class="delete" />
</td>
</tr>
</tbody>
</table>
<script src="static/jquery-3.6.0.min.js"></script>
<script>
$(function(){
$(".delete").click(function(){
$(this).parent().remove();
});
})
</script>
</body>