路徑我想使用的值從數組來填充網頁。該值應該更換跨度之間的文本(這個已經工作),但在同一時間從陣列中的一些值應作爲屬性和作爲文件路徑的一部分。最後,只有當某個值符合某個條件時才應該更換。插入文本,屬性和條件
插入陣列中的數據以不同的方式 - 這怎麼可能?
這是HTML部分:
<p><b><span class="weather">weather here</span></b> and
<span class="temperature">temperature here</span>.</p>
<p><i><span class="color">color here</span></i>.</p>
Here follows is an image loaded according to the data
<img src="fixed_path#weather"></img>. And this should
have the proper <span color="#color">hue</span>.
<span class="warning"></span>
這是jQuery的JavaScript部分(的jsfiddle鏈接如下):
var arr = {
"weather": "cloudy",
"color": "#880000",
"temperature": "hot"
};
$.each(arr, function (key, value) {
$('.'+key).replaceWith(value);
// how to replace src path?
// how to replace text attribute?
// make the following conditional
// if($.inArray("temperature.hot", arr) > !=1) {
$('.warning').replaceWith('Warning!');
// }
});
你需要改變你的數組中元素的結構。在這種情況下,應該如何確定「文本」的含義? – Bergi 2013-03-17 16:48:17
我改變了陣列。問題保持不變。我猜有條件的插入不會這樣工作,因爲關聯(字符串)數組在JavaScript中是未知的。對於其他的事情,我仍然在尋找答案。 – PiEnthusiast 2013-03-17 20:16:51
它和以前一樣工作(雖然它不再是一個數組,而是一個無序的對象)。而且在結構中還沒有關於是否改變'src','color'或'textContent'屬性的信息。 – Bergi 2013-03-17 20:31:50