【发布时间】:2019-05-21 11:00:43
【问题描述】:
我有一个用于加载数据并附加到 div 的 xml 函数。
div.thumb img 的 css 和 jQuery 没有选择图像。
JavaScript:
$(document).ready(function () {
function loadfail() { alert("Error: Failed to read file!"); }
//Load xml data
function parse(document) {
$("#thumbs").append('<div class="thumb">');
$(document).find("entry").each(function () {
var image = $(this).find('image').text();
$("#thumbs").append('<img src="Styles/images/' + image + '" width="64" height="64" />');
});
$("#thumbs").append('</div>');
$('div.thumb img').click(function () {
$('#expandedimage').slideToggle(1000);
});
}
$.ajax({
url: 'appdata/data.xml', // name of file with our data
dataType: 'xml', // type of file we will be reading
success: parse, // name of function to call when done reading file
error: loadfail // name of function to call when failed to read
});
});
CSS:
div.thumb { float:left ; padding: 1px; }
div.thumb img { border: 2px solid white; }
HTML 标记:
<div id="body">
<div id="expandedimage">
Toggled Image
</div>
<hr />
<div id="thumbholder">
<div id="thumbs">
<!-- image(s) here! -->
</div>
<hr />
</div>
</div>
代码在每个图像放置一个 div 时起作用,而不是将所有图像放在一个 div 中:
$("#thumbs").append('<div class="thumb"><img src="Styles/images/' + image + '" width="64" height="64" /></div>');
但是,第二个<br /> 没有出现图像。
那么我的问题是,为什么当我有一个 div 和该 div 中的图像时,div.thumb img 不起作用。
【问题讨论】:
-
这篇文章没有意义:
$("#thumbs").append('</div>');你不能只附加一个结束标签或一个开始标签,所有元素都作为带有开始和结束标签的整个元素附加,或者自动关闭。
标签: jquery html css css-selectors