我正在嘗試查找類別爲pic
的圖片,如果圖片不存在,我想補充一些任務,如果確實存在,我不想做任何事情。我試過以下代碼,但沒有成功。無論我做什麼,我都會收到警報。如果圖片不存在,請添加新元素
IMG代碼
<img src="" class="pic">
jQuery的
$(".stxt, .stxt2").each(function() {
if ($(this).find("img.pic").length === 0) {
$(document).ready(function() {
var unitSize = 10; // width (and height) of one square
var unitsWide = 6; // number of squares along x-axis
var unitsTall = 6; // number of squares along y-axis
var drawing = $('<div class="drawing"></div>').css({
width: unitSize * unitsWide
});
for (var i = 0; i < unitsWide * unitsTall; i++) {
var randomColor;
randomColor = Math.random() * 0x1000000; // 0 < randomColor < 0x1000000
randomColor = Math.floor(randomColor); // 0 < randomColor <= 0xFF5F0FF
randomColor = randomColor.toString(16); // hex representation randomColor
randomColor = ("000000" + randomColor).slice(-6); // leading zeros added
randomColor = "#" + randomColor; // # added
$('<span class="square"></span>').css({
display: 'block',
float: 'left',
width: unitSize,
height: unitSize,
'background-color': randomColor
}).appendTo(drawing);
}
drawing.insertBefore($(this).find("div.dtxt2, div.dtxt"));
});
}
});
你從哪兒打電話呢?這是什麼? – Dogbert
我不認爲你想把$(document).ready()放在each()裏面。看起來如果你找不到圖片,你正在放置一個60x60彩色塊嗎?一些HTML可能會有所幫助。 – Dennis