问题背景:

就是网站的“搜索功能”
当在一个input里面输入信息后,给一个div设置一个点击事件,后去input里面的值,之后跳转进行后台操作

操作流程

  1. 引入jQuery
    在head 标签里面添加:
<script src="http://libs.baidu/jquery/2.1.4/jquery.min.js"></script>
  1. 建一个js文件,写js代码
function toSubmit() {
    var content = $('#detail').val();
    $(window).attr('location', '/MainServlet?action=search' + '&content'+ content + '');
}
  1. 引入js文件,给div添加onclick事件
<script type="text/javascript" src="../js/js1.js"></script>

<div class="u-search-icon"  id="submit"  onclick="toSubmit()">

报错及细节解答

  • 注意:要先引入jQuery,后引入你写的js文件(也就是在head里面:“引入jQuery的语句”要放在“引用你自己写的js文件”  之前
    不然可能出现报错:Uncaught ReferenceError: $ is not defined
  • 对于:var content = $(’#detail’).val();
    $(window).attr(‘location’, ‘/MainServlet?action=search’ + ‘&content’+ content + ‘’);;
    的解读 相关链接:
    https://www.runoob/jquery/jquery-selectors.html
    https://www.runoob/jquery/jquery-dom-get.html
    https://wwwblogs/pythonywy/p/11408329.html

最后,还有哪些不足之处,请下方留言指正

更多推荐

java web之给一个div设置点击事件并进行页面跳转(功能是:实现“搜索”的功能)