路径我想使用的值从数组来填充网页。该值应该更换跨度之间的文本(这个已经工作),但在同一时间从阵列中的一些值应作为属性和作为文件路径的一部分。最后,只有当某个值符合某个条件时才应该更换。插入文本,属性和条件
插入阵列中的数据以不同的方式 - 这怎么可能?
这是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