我加入新李 & 格在頁面上元素的jQuery .html()
功能的手段。我傳遞的字符串有一個硬編碼的類。但它仍然沒有風格化,但類已經在css文件中定義了。所以我通過使用jQuery .css()
函數添加樣式,但仍然沒有樣式。我怎樣才能添加CSS到這些動態添加的HTML元素?jQuery的添加CSS來動態添加HTML元素
for(i=0; i<directionList.length; i++) {
html += "<li class='dir-list'> \
<div class='dir-loc'><b>" + directionList[i].location + "</b></div><br /> \
<div class='dir-dest'> \
<div class='dir-dest-icon'><img src='markers/new/transit-small.gif' class='dir-va-mid' title='by transit'/></div> \
<div class='dir-dest-action'><span class='dir'>Ride to</span></div> \
<div class='dir-dest-loc'><span class='dir'><b>" + directionList[i].destination + "</b></span></div> \
<ul class='dir-routes'>";
// add routes
var routes = directionList[i].routes;
for(j=0; j<routes.length; j++){
html += "<li> \
<div class='dir-route-type-icon'><img src='markers/new/" + routes[j].type + "-small.gif' class='dir-va-mid' title='" + routes[j].type + "'/></div> \
<div class='dir-route-name'><span class='dir-va-mid' >" + routes[j].name + "</span></div> \
</li>";
}
html += "</ul> \
</div> \
</li>";
/*
var index = parseFloat(i)+1;
directions += "<li>" + directionList[i].location + "<span>";
//get routes
var routes = [];
routes = directionList[i].routes;
directions += "<ul>";
for(j=0; j<routes.length; j++){
directions += "<li> (" + routes[j].type + ") " + routes[j].name + "</li>" ;
}
directions += "</ul>";
directions += "</li>";
*/
}
html += "</ul>";
$('#directions').html(html);
CSS:
#directions div {
display:inline;
vertical-align:middle;
}
#directions ul#dir {
list-style:decimal;
}
#directions ul.dir-routes {
list-style: none;
}
.dir-va-mid {
vertical-align:middle;
}
強行添加CSS:
$("#directions div").css({'display':'inline', 'vertical-align':'middle'});
$("#directions ul#dir").css({'list-style':'decimal'});
$("#directions ul.dir-routes").css({'list-style':'none'});
$(".dir-va-mid").css({'vertical-align':'middle'});
編輯:我已經列入我的文件here。如果你有時間,請看看它。
請張貼一些代碼。這聽起來像你通過jQuery添加的元素根本不匹配你現有的CSS選擇器。 – Dominic 2011-02-27 21:15:59
「一些」代碼的概念似乎對每個人都不清楚:) – 2011-02-27 21:29:46