案例:大量字串拼接效果實作
按鈕點擊,字串拼接,最后效果字串,str
input有很多,type來分就有button和text,需要找出inputs[i].value是text的
所以用!="button",滿足條件就push進str,所以是str.push(inputs[i].value)
console.log顯示,用str.join就可拼接,加個|清楚一點
簡而言之,遍歷順便拿到inputs[i].value不是按鈕的,push進str, 最后join進str
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>title</title> </head> <body> <input type="button" value="拼接吧" id="btn" /><br /> <input type="text" value="" /><br /> <input type="text" value="" /><br /> <input type="text" value="" /><br /> <input type="text" value="" /><br /> <input type="text" value="" /><br /> <script src="common.js"></script> <script> //推薦使用陣列的方式拼接大量的字串 document.getElementById("btn").onclick = function () { var str = []; //獲取所有的文本框 var inputs = document.getElementsByTagName("input") //每個文本框的value屬性值 for (var i = 0; i < inputs.length; i++) { if (inputs[i].type != "button") { str.push(inputs[i].value); } } console.log(str.join("|"));//字串 }; </script> </body> </html>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/127144.html
標籤:其他
