<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鳥教程(runoob.com)</title>
<script>
function formSubmit(){
var searchValue=document.getElementById("kw").value;//獲取搜索框內容
var submitValue=searchValue+"+site:shuobo.info";//拼接百度搜索打開的鏈接
console.log(submitValue)
var a=document.createElement('a');//創建一個超鏈接a標籤,按鈕submit時,觸發點擊超級鏈接動作
var href="https://m.baidu.com/s?word=" + submitValue;//百度搜索需要打開的完整鏈接
console.log(href);
a.setAttribute('href',href);//爲超鏈接添加href屬性;
a.setAttribute('target','_blank');//點擊超鏈接時打開新的頁面;
a.setAttribute('id','myLink');//爲超鏈接添加id
if(document.getElementById("myLink")){//防止重複添加,如果頁面中已經存在相同id的超鏈接,則刪除
console.log("ififif");
document.body.removeChild(document.getElementById("myLink"));
}
document.body.appendChild(a);//把創建的超鏈接添加到頁面
a.click();//模擬觸發點擊超鏈接的click動作
}
</script>
</head>
<body>
<section class="head-search" data-class="text">
<form id="search" class="search-form search hd_search" >
<input type="text" name="word" id="kw" class="search-ipt sh_ipt" placeholder="搜你想搜">
<input type="button" onclick="formSubmit()" value="百度一下">
</form>
</section>
</body>
</html>