0

这就是问题所在;我有一个 div,它本身包含子 div,我似乎无法访问子 div。我需要的是为每个子 div 找到带有 .eq90 的索引。

这是生成父级的代码。

function makeSmallBlockdiv ()
{
var smallBlock = $('<div class="SmallBlock EditBlock"></div>').appendTo("#canvas");
smallBlock.draggable({containment: "#canvas", scroll: false, grid: [10, 10]}, {cursor: "move", cursorAt: {top: 125, left: 150}})
smallBlock.append('<div class="article_title EditBlock fontCenter fontBold font24">Article Title</div>')
smallBlock.append('<div class="article_Image"><img style="width: 250px;" src="<? echo $image1 ?>"></div>')
smallBlock.append('<div class=" article_text font14"><? echo substr($article_text, 0, 200) ?></div>')   
}

可以根据需要经常调用此函数以创建更多父 div。到目前为止,我已经尝试过以下内容:

$(".article_text").click(function() {
  alert("Index: " + $(this).index());
});

我已经将它包装在一个带有更改的函数 ready() 中。这段代码来自我在这里找到的小提琴http://jsfiddle.net/fY6SU/48/

小提琴代码完美运行,但我的代码什么也没做。当我用 firebug 查看我的 html 时,我可以看到 div 已正确创建。我很困惑,我刚开始使用 jquery 并没有帮助。

任何建议将不胜感激。

谢谢

克里斯

4

4 回答 4

2
$(".article_text").click(function() {
  alert("Index: " + $(this).index());
});

选择器只选择了“article_text”类,而你生成的html只包含一个带有“article_text”类的div,所以解决方法是在同名的div中添加一个类或使用多个选择器,如下所示:

$(".article_text, .article_Image, .article_title, .SmallBlock").click(function() {
  alert("Index: " + $(this).index());
});
于 2012-12-13T18:52:11.017 回答
0

What I need is to find the index with .eq90 for each of the child divs.

这里:

var ninetyChildren = new Array(); //create an array to hold the children
$(".parent").each(function() { //loop through each parent
    var ninety = $(this).children().eq(89).text(); //get index of 90 - eq starts with 0
    ninetyChildren.push(ninety); //add 90th index to array
});

$.each(ninetyChildren, function(i, data){ //loop through 90th indexes
alert(i +" "+ data); //alert each 90th index
});
​

希望这是你想要的。 模拟演示

于 2012-12-13T19:24:24.220 回答
0

我能够弄清楚如何为兄弟姐妹获取 index() 。

这是我用来查找它们的代码。

$("div").children(".article_text").eq(3).css("color", "red");

css 选择器只是作为确认其工作的视觉辅助。

感谢所有帮助过我的人。

于 2012-12-13T19:28:43.320 回答
0

您的解决方案在于您的班级名称。$(".article_text") 仅选​​择最后一个 div 和 ,考虑根据示例小提琴中的类名重命名

于 2012-12-13T18:57:11.050 回答