js 字符串分割成字符串数组 遍历数组插入指定DOM里 原生JS效果

使用的TP3.2

JS字符串分割成字符串数组

 var images='{$content.pictureurl} ' ;
结构是这样
attachment/picture/uploadify/20141126/547550278b7db.jpg,attachment/picture/uploadify/20141126/54755027ab89b.jpg,attachment/picture/uploadify/20141126/547550273b753.jpg,attachment/picture/uploadify/20141126/54755027d8488.jpg

 

HTML

 <p id="con"></p>

JS

 <script type="text/javascript">
        /*
        * 字符串 分割成字符串数组 并动态添加到指定ID的DOM 里
        * @id 要插入到DOM元素的ID
        *
        * 输入值为图片URL 字符串
        * */
        function addImages(id){
            /*字符串 变量*/
            var images='{$content.pictureurl} ' ;
            console.log( images ) ;
            /*字符串分割成字符串数组 split*/
            var StringArray = images.split(',');
            console.log( StringArray ) ;
            /*遍历 字符串数组 并动态插入到DOM里*/
            for(i=0,ii=StringArray.length;i<ii;i++){
                console.log(StringArray.length);
                var  imageURL =StringArray[i];
                console.log( imageURL);

               var ele= document.getElementById(id);
                /*叠加内容*/
               ele .innerHTML+="<img src="+'/'+imageURL+''+">";
             }

        }
        addImages("con");
    </script>

 

posted @ 2014-11-28 14:54  星耀学园  阅读(1226)  评论(0编辑  收藏  举报