六、HTML DOM遍历和修改
对于Dom的修改上文已经进行了总结,现在说下遍历
1.遍历祖先: parent ,parents,parentsUntil
parent:会向上遍历,返回上一级父级
parents:会向上遍历返回所有父级
parentsUntil(‘元素’):向上遍历,返回到传入元素中所有的父级(不包含传入元素)
111
2.遍历后代: children,find
children:遍历下一级所有的后代子元素
find:遍历所有的后代子元素(需要传入选择器条件,不传返回空)
111
3.同级遍历: siblings(),next(),nextAll(),nextUntil(),prev(),prevAll(),prevUntil()
4.元素过滤:first,last,eq,filter
first:元素集合中过滤出第一个元素,
last:从元素集合中过滤出最后一个元素
eq:传输参数数字n,从元素集合中过滤出第n个元素
filter:传入过滤条件,按条件过滤。
七、ajax:
1.$('元素对象').load(“资源路径”,callback):加载服务器端的资源放入指定元素里
这里可以用这个方法将html中的内容抽取出来,加入到需要的部分
2.$.get(),$.post
$.get(url,callback):向服务端请求数据
$.post(url,data,callback):客户端向服务端提交数据
在做测试的时候遇到一个405 method not allowed,检查了路径没有错误,甚至重新写了servlet后来我将后台的doget改成了dopost数据才请求成功
请求数据:
public class GetServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
List peoliList = new ArrayList<>();
People people = new People("array", "hehe", 18, 17899.0);
People people2 = new People("jcy", "1", 18, 1117899.0);
People people3 = new People("obj", "obj", 22, 12563.3);
peoliList.add(people);
peoliList.add(people2);
peoliList.add(people3);
String string = "hello get";
resp.getWriter().print(peoliList.toString());
}
}
提交数据:
package com.array.servlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.array.bean.People;
@WebServlet("/post")
public class PostServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String username = req.getParameter("username");
Integer age = Integer.valueOf(req.getParameter("age"));
System.out.println(username+"年龄是"+age);
resp.getWriter().print("hahaha");
}
}