jQuery 如何使用类创建div

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7833701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 00:19:15  来源:igfitidea点击:

How to create div with class

jquerycreation

提问by fteinz

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

我正在尝试创建一个 div 并给他一个课程,但它不起作用。有人可以帮我吗?

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

css:

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

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

在他创建 div 的那一刻,但颜色不是 #eeeeee

回答by Corneliu

use "class" instead of className

使用“类”而不是类名

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

回答by alexl

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

http://jsfiddle.net/yF9pA/1/

回答by Dipak

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

回答by Marco Johannesen

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

应该管用 :)

回答by Uku Loskit

try classinstead of className

尝试class代替className