<!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>