這個功能在百度其實已經做得非常好了,這里借花獻佛,記錄下使用經驗
1.效果
2.在div內嵌入
<div style='overflow:hidden'> <form onsubmit="return baiduWithHttps(this)" action="http://www.baidu.com/baidu" target="_blank"> <input name="tn" type="hidden" value="SE_zzsearchcode_shhzc78w"> <input class='bdtext' type="text" onfocus="checkHttps" name="word" size="30"> <input class='bdbtn' type="submit"value="百度搜索"> </form> <script src="http://s1.bdstatic.com/r/www/cache/global/js/BaiduHttps_20150714_zhanzhang.js"></script> <script> function checkHttps () { BaiduHttps.useHttps(); }; function baiduWithHttps (formname) { var data = BaiduHttps.useHttps(); if (data.s === 0) { return true; } else { formname.action = 'https://www.baidu.com/baidu' + '?ssl_s=1&ssl_c' + data.ssl_code; return true; } }; </script> </div>
3.這樣在頁面上看到的效果,input使用的是默認樣式,這里把百度樣式移植了過來
input.bdbtn {width: 65px;height: 28px;line-height: 28px;font: 12px \5b8b\4f53;cursor: pointer;outline: none;display: inline-block;text-align: center;color: #fff;border: none;background-color: #1a89ed;} input.bdtext {width: 240px;padding-left: 5px;margin-left: 10px;margin-right: 10px;height: 22px;line-height: 22px;}
如果出現了亂碼使用一下方法
提示:如果網頁采用utf-8代碼,則需要在代碼中加入 <input name=ie type=hidden value=utf-8> ,否則搜索關鍵詞會出現亂碼。