問題背景:
就是網站的“搜索功能”
當在一個input裏面輸入信息後,給一個div設置一個點擊事件,後去input裏面的值,之後跳轉進行後臺操作
操作流程
- 引入jQuery
在head 標籤裏面添加:
<script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
- 建一個js文件,寫js代碼
function toSubmit() {
var content = $('#detail').val();
$(window).attr('location', '/MainServlet?action=search' + '&content'+ content + '');
}
- 引入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.com/jquery/jquery-selectors.html
https://www.runoob.com/jquery/jquery-dom-get.html
https://www.cnblogs.com/pythonywy/p/11408329.html
最後,還有哪些不足之處,請下方留言指正