如何使用JavaScript将每个字符从字符串写入单独的div?

时间:2017-12-18 11:18:08

标签: javascript

这就是我一直以来的目标

这是我的代码:

group_id

代码仅适用于删除循环:

function createWordTiles() {
            var word = ['Example'];
            var innerDiv;

            var iDiv = document.createElement('div');
            iDiv.id = 'tileBlock';
            iDiv.className = "tileBlock";
  

这就是我所做的

如何使用JavaScript将每个字符从字符串写入单独的div?

2 个答案:

答案 0 :(得分:4)

将你的iDiv.appendChild(innerDiv);移到for循环中,如下所示:

for (var l = 0; l <= word.length ; l++) {

    innerDiv = document.createElement('div');
    innerDiv.id = 'Block' + l;
    innerDiv.className = 'Block';
    innerDiv.innerHTML = word.charAt(l);

    iDiv.appendChild(innerDiv);
}

答案 1 :(得分:2)

HTML

jQuery(document).ready(function($) {
var sourceText = $('.source-text').text(); // get the source text
var $destinationElm = $('.list'); // get destination 

var i;
for(i = 0; i < sourceText.length; i++) {  // eterate through all characters
    $destinationElm.append('<li>' + sourceText[i] + '</li>'); // appends html element with text
}});

JS

from django.core.mail import send_mail
send_mail('test email', 'hello world', 'xxxx@gmail.com', ['xxxx@gmail.com'],fail_silently=False)

# Email settings
EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_PASSWORD = 'xxxx' #my gmail password
EMAIL_HOST_USER = 'xxxx@gmail.com' #my gmail username
EMAIL_PORT = 587
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER