Cómo crear div con clase


Estoy tratando de crear un div y darle una clase, pero no funciona. ¿Alguien puede ayudarme?

$(document).ready(function() {
$('input[type=checkbox]').each(function() {
    $(this).after($('<div />', {
        className: 'test',
        text: "a div",
        click: function(e){
            e.preventDefault();
            alert("test")
        }})); 
    });
});

El css:

   .test {
    width:200px;
    height:200px;
    background-color:#eeeeee;
    }

En este momento crea el div pero el color no es # eeeeee

Author: Bo Persson, 2011-10-20

5 answers

Use "class" en lugar de className

$('<div />', {
        "class": 'test',
        text: "a div",
        click: function(e){
            e.preventDefault();
            alert("test")
        }})
 53
Author: Corneliu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-10-20 09:27:05
$(document).ready(function() {
$('input[type=checkbox]').each(function() {
    $(this).after($('<div />', {
        class: 'test',
        text: "a div",
        click: function(e){
            e.preventDefault();
            alert("test")
        }}));
    });
});

Http://jsfiddle.net/yF9pA/1 /

 6
Author: alexl,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-10-20 09:27:28
 $('<div>', { 'class': 'your_class' })
                 .load('HTML Structure', CallBackFunction())
                 .appendTo(document.body);
 4
Author: Piccaza,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-10-13 15:39:11
$('input[type=checkbox]').each(function() {

$(this).after('<div></div>').addClass('test')
  .filter('div').html('a div')
.click(function() {
  alert('Handler for .click() called.');
}).end()
.appendTo('this');

});

Debería funcionar:)

 3
Author: Marco Johannesen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-10-20 09:30:45

Intenta class en lugar de className

 2
Author: Uku Loskit,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-10-20 09:23:02