前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >JavaScript split() 方法

JavaScript split() 方法

作者头像
王小婷
发布2021-03-15 21:38:57
8690
发布2021-03-15 21:38:57
举报
文章被收录于专栏:编程微刊

定义和用法

split() 方法用于把一个字符串分割成字符串数组。

运用: 将json数据里面的字符串类型的数据"00,01,02,03,04"转化成数组,再将数组遍历出来,分别赋值到每一个div里面。

json数据

代码语言:javascript
复制
{

    "tilesFloor": {
        "id": 0,
        "paramName": "tilesFloor",
        "paramValue": "00,01,02,03,04"
        
    }
    
}

代码

代码语言:javascript
复制
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
    <head>
        <title></title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link rel="stylesheet" href="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
        <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
        <script src="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
        <style>
            /* 楼层条样式 */
            .floorBar {
                position: absolute;
                right: 30%;
                top: 10%;
                color: #fff;
                z-index: 1000;
                width: 29px;
                height: 150px;
                text-align: center;
                box-shadow: 0px 0px 10px #0076ff inset;
                cursor: pointer;
                overflow: auto;
                background: rgba(39, 32, 97, 0.5);
                font-size: 12px;
            }
            
            .floorinside {
                line-height: 31px;
                height: 30px;
            }
    </style>
    </head>
    <body>
        <div class="floorBar"></div>
    </body>
    <script type="text/javascript">
        $.ajax({
            url: "test.json",
            data: {},
            type: "GET",
            success: function(data) {
                var html = "";
                $.each(data.tilesFloor.paramValue.split(','), function(i, item) {
                    html += '<div  class="floorinside">F' + item + '</div>';
                });

                $(".floorBar").html(html);
            },
            error: function(err) {
                console.log(err);
            }
        })
    </script>
</html>

样式

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档