<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<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>
// 通过数组拼接
// 点击按钮实现拼接
document.getElementById("btn").onclick =function () {
//定义一个空对象;
var str = [];
// 获取全部的文本框值
var inputs = document.getElementsByTagName("input");
// 每个文本框的值 因为多个文本框需要for循环遍历
for(var i =0;i<inputs.length;i++ ) {
// 因为获取的文本框内有一个是按钮所有需要判断去除
if(inputs[i].type != "button" ) {
str.push(inputs[i].value);
}
}
// 输出
console.log(str.join("|"));
};
// 通过字符串拼接
// 点击按钮实现拼接
document.getElementById("btn").onclick = function () {
//定义一个空
var str = "";
// 获取全部的文本框值
var inputs = document.getElementsByTagName("input");
// 每个文本框的值 因为多个文本框需要for循环遍历
for(var i =0;i<inputs.length-1;i++ ) {
// 因为获取的文本框内有一个是按钮所有需要判断去除
if(inputs[i].type != "button" ) {
str += inputs[i].value + "|";
}
}
// 输出
console.log(str+inputs[inputs.length-1].value);
};
</script>
</body>
</html>
js字符串拼接
最新推荐文章于 2024-03-17 09:15:00 发布