How to create div with class

巧了我就是萌 提交于 2019-12-31 10:19:10

问题


I'm trying to create a div and give him a class but it doesn't work. Could anybody help me?

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

The css:

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

at the moment he creates the div but the color isn't #eeeeee


回答1:


use "class" instead of className

$('<div />', {
        "class": 'test',
        text: "a div",
        click: function(e){
            e.preventDefault();
            alert("test")
        }})



回答2:


$(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/




回答3:


 $('<div>', { 'class': 'your_class' })
                 .load('HTML Structure', CallBackFunction())
                 .appendTo(document.body);



回答4:


$('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');

});

Should work :)




回答5:


try class instead of className



来源:https://stackoverflow.com/questions/7833701/how-to-create-div-with-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!