繁体   English   中英

创建DIV元素并添加HTML内容

[英]Creating DIV elements and adding HTML Contents

我对jquery不了解。 某些机构可以指导我下面的代码做什么。 我用谷歌搜索,但没有找到结果。 我想在div类为'p-notification'的div中添加一些HTML内容。 这个怎么做?

 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'
 });

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

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

下一部分将在范围上添加或设置content属性为一个新创建的div元素,并预先设置了几个属性: classstyletext

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

就其本身而言,这不会对DOM产生任何影响,因为您已经创建了元素但未将其插入文档中。 您可以使用jQuery提供的几种方法之一来插入它们,例如append

$('#somediv').append(this.content);//adds the div that was created to an element with id somediv

您可以这样添加内容:

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

或替换内容:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM