我有一個問題,在JQuery中,動態添加的DOM節點不採用CSS文件中定義的樣式。JQuery動態添加DOM丟失(不)CSS樣式
我環顧四周,並the answers I've seen,不解決問題。也就是說,相關的CSS位於相對於主機頁面的路徑中。這絕對是在記憶中。但是,如果我使用A)或B),附加或加載的'newNode'不包含已經加載的樣式。
A)$("#some-selector").append(newNode);
B)$("#some-selector").load("/path/to/newNode.html");
我發現我必須手動添加的風格像
C)$(".loadedElement").css('some-style','some-value');
有沒有人解決了這個?
在此先感謝。
---編輯
所以我進一步了。我有一個HTML像A)和CSS像B)。事實證明,嵌套的CSS選擇器不匹配我想要的節點。我不得不像C)那樣去選擇一個選擇器。然後我的動態節點應用了這種風格。但我真的希望有像B)的東西。我的CSS關閉了嗎?
A)
<div id='6bullet-container'>
<input id='six-bullet-image-upload' name='convert' type='file'>
<div class='six-bullet-content-wrapper'>
<div class='six-bullet-container-label'>
click to upload
</div>
<div id='six-bullet-title'></div>
<div class='six-bullet-bullet' id='six-bullet-one'></div>
<div class='six-bullet-bullet' id='six-bullet-two'></div>
<div class='six-bullet-bullet' id='six-bullet-three'></div>
<div class='six-bullet-bullet' id='six-bullet-four'></div>
<div class='six-bullet-bullet' id='six-bullet-five'></div>
<div class='six-bullet-bullet' id='six-bullet-six'></div>
…
B)
#6bullet-container #six-bullet-image-upload .six-bullet-content-wrapper .six-bullet-bullet {
my: style;
}
C)
.six-bullet-bullet {
my: style;
}
無論元素如何在DOM中結束,它們都會應用相同的樣式。您的新元素與您的CSS選擇器相匹配存在一些問題,但由於您既未提供元素也未提供CSS,因此我無法提供幫助。我假設你已經確認直接編碼到HTML中的元素可以獲得正確的樣式? – 2012-03-01 16:49:20
@amnotiam關於CSS選擇器是正確的。它可能也是,當你把你的html放入$(「#some-selector」)時,樣式並沒有被正確地定義,更好的檢查$(「#some-selector」)。load(「/ path/to/newNode.html」,功能(數據){的console.log(數據)});確切地看到你從服務器上得到什麼 – Liviu 2012-03-01 17:12:49
你能告訴我們你的CSS以防萬一嗎? – MMM 2012-03-01 17:24:15