<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> 1.jsonp a.js爲服務器數據,是一個函數調用,具體的函數在客戶端 <script> function show (data) { console.log(data.name, data.age); } </script> <script> // 服務端函數調用 // a.js show({ name: 'kang', age: 23 }); </script> <hr> 2.百度搜索是一個jsonp,引入後,本地實現show2方法 <script> function show2 (data) { console.log(data.s); } </script> <script src="https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=qq&cb=show2" charset="utf-8"></script> <hr> 3.引入後端的jsonp,本地實現方法 <script> function show ({s}) { console.log(s); } window.onload = function () { let oTxt = document.getElementById('txt1'); oTxt.oninput = function () { let url = `https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=${this.value}&cb=show`; let oS = document.createElement('script'); oS.src = url; oS.onload = function () { oS.parentNode.removeChild(oS); // 追加script,執行完又刪掉 }; document.head.appendChild(oS); }; }; </script> <input type="text" id="txt1"> </body> </html>