1、在整个jquery开发框架之中,最为重要的工具就是页面元素的选择处理。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
pageContext.setAttribute("APP_PATH",request.getContextPath());
%>
<script type="text/javascript" src="${APP_PATH}/static/js/jquery-3.2.1.min.js"></script>
<link rel="stylesheet" href="${APP_PATH}/static/bootstrap-3.3.7-dist/css/bootstrap.min.css" />
<script type="text/javascript" src="${APP_PATH}/static/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
<script type="text/javascript">
function fun() {
var msgValue = $(msg).val();
console.log("**************msg=" + msgValue);
}
</script>
<html>
<body>
<input type="text" name="msg" id="msg">
<input type="button" value="选择" onclick="fun()">
</body>
</html> <script type="text/javascript" src="${APP_PATH}/static/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script> <script type="text/javascript"> function fun() { var msgValue = $(msg).val(); $(showMsg).text(msgValue); } </script> <html> <body> <input type="text" name="msg" id="msg"> <input type="button" value="设置内容" onclick="fun()"> <div id="showMsg"></div> </body> </html> 5、但是text()设置的都是纯文本,如果此时你输入的内容包含有html元素。 <script type="text/javascript" src="${APP_PATH}/static/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script> <script type="text/javascript"> function fun() { var msgValue = $(msg).val(); $(showMsg).html(msgValue); } </script> <html> <body> <input type="text" name="msg" id="msg"> <input type="button" value="设置内容" onclick="fun()"> <div id="showMsg"></div> </body> </html> <script type="text/javascript" src="${APP_PATH}/static/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script> <script type="text/javascript"> function fun() { console.log($(showMsg).text()); console.log($(showMsg).html()); } </script> <html> <body> <input type="text" name="msg" id="msg"> <input type="button" value="设置内容" onclick="fun()"> <div id="showMsg"><h1>www.baidu.com</h1></div> </body> </html> 8、面试题:请问text()和html()有什么区别?


