当前位置:网站首页>js中的join()方法
js中的join()方法
2022-08-02 14:20:00 【铃儿响叮当不响】
join()方法就是将array数据中每个元素都转为字符串,用自定义的连接符分割
1、join('')将数组元素无缝拼接
<script>
let s = Array('a','p','p','l','e')
document.write(s.join(''))
</script>
输出结果:apple
2、join(' ') 将数组元素以空格分割
<script>
let s = Array('Apple','is','on','my','table')
document.write(s.join(' '))
</script>
输出结果:Apple is on my table
3、join()将数组每个元素都转为字符串,用法等同于toString()
<script>
let s = Array(1,2,3)
console.log(s)
console.log(s.join())
</script>
4、join()将数组转换为页面元素的内容
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<ul id="list"></ul>
<script>
let data = ['第一个','第二个','第三个','第四个']
let list = document.querySelector('#list')
let content = '<li>' + data.join('</li><li>') + '</li>'
list.innerHTML = content
</script>
</body>
</html>
案例代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body style="padding: 20px;">
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>id</th>
<th>书名</th>
<th>作者</th>
<th>出版社</th>
<th>操作</th>
</tr>
</thead>
<tbody id="tb">
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<script>
let tb = document.querySelector('#tb')
data = [
{id:1, bookname:'红楼梦', author:'曹雪芹', publisher:'河南出版社'},
{id:2, bookname:'活着', author:'余华', publisher:'海南出版社'},
{id:3, bookname:'我与地坛', author:'史铁生', publisher:'湖南出版社'}
]
let content = []
for(i = 0; i < data.length; i++){
content.push('<tr><td>'+data[i].id+'</td><td>'+data[i].bookname+'</td><td>'+data[i].author+'</td><td>'+data[i].publisher+'</td><td><a href="javascript:;">删除</a></td></tr>')
tb.innerHTML = content.join('')
}
</script>
</body>
</html>
</html>
边栏推荐
- The DOM event type
- DOM —— 事件绑定与解绑
- webrtc 数据接收流程图解
- synchronized详解
- 网络运维系列:远程服务器登录、配置与管理
- Explain in detail how the bit manipulation operators in C language can be used?
- 在命令行或者pycharm安装库时出现:ModuleNotFoundError: No module named ‘pip‘ 解决方法
- 网络运维系列:端口占用、端口开启检测
- 抽象队列同步器AQS应用Lock详解
- LAMP 环境搭建 yum源安装方式 (Apache 2.4.6 +mysql 8.0.28+php 8.1.3)
猜你喜欢
随机推荐
DOM —— 页面的渲染流程
Mysql删库恢复数据
golang中使用泛型
makefile——library
smart_rtmpd 轻松突破 C100K 测试
在命令行或者pycharm安装库时出现:ModuleNotFoundError: No module named ‘pip‘ 解决方法
解决启动filebeat时遇到Exiting: error unpacking config data: more than one namespace configured accessing错误
web渗透之sql注入
Based on the SVM regression forecast 】 【 LibSVM realize the prediction of a characteristic data
nvm详细安装步骤以及使用(window10系统)
smart_rtmpd 的 VOD 接口使用说明
DOM - page rendering process
解决跨域的方法 --- Proxy
【Anaconda】一行语句解决Spyder启动问题
lammps学习(二)联合原子模型聚乙烯拉伸
GC垃圾回收ZGC
基于Visual Studio 2015的CUDA编程(一):基本配置
flask获取post请求参数
filebeat的配置
CSV file with the data read and write 】 【 XLS/XLSX file