创建DIV元素并添加HTML内容

时间:2012-11-10 11:08:48

标签: javascript jquery

我对jquery一无所知。有些人可以指导下面代码的作用。我用Google搜索但没有找到结果。我想在div的类中添加一些HTML内容 'P-通知'。怎么做?

 this.notification = $('<div/>', {
            'class': 'p-notification'
 });
 this.content = $('<div/>', {
        'class': $elem.attr("hasFooter") === "true" ? 'p-content with-p-footer' : 'well',
        'style': $elem.attr("paddless") === "true" ? 'padding:0;' : '',     
        'text': 'Loading'
 });

2 个答案:

答案 0 :(得分:1)

以下部分将范围上的notification属性添加或设置为具有类div的新创建的p-notification元素:

 this.notification = $('<div/>', {
            'class': 'p-notification'
 });

下一部分将范围上的content属性添加或设置为新创建的div元素,其中预设了多个属性:classstyle和{ {1}}:

text

由于您已经创建了元素但未将它们插入到文档中,因此它本身不会对DOM产生任何影响。您可以使用jQuery提供的several methods之一插入它们,例如 this.content = $('<div/>', { 'class': $elem.attr("hasFooter") === "true" ? 'p-content with-p-footer' : 'well', 'style': $elem.attr("paddless") === "true" ? 'padding:0;' : '', 'text': 'Loading' });

append

答案 1 :(得分:1)

你可以这样做来添加内容:

$('.p-notification').append('<div>Hey</div>');

或者这个替换内容:

$('.p-notification').html('<div>Hey</div>');
相关问题