[英]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
元素,並預先設置了幾個屬性: class
, style
和text
:
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.