在 jQuery 中,我怎样才能正确地将选择器用作 for 循环中的变量
In jQuery how can I correctly use a selector as var in a for loop?
当应用于for循环中动态创建的div(从XML文件)时,我正在尝试在我的选择器中使用变量。预览对话框仅在我单击最后一个div 时打开。如何让预览对话框在我单击每个概述div 时打开?
<script type="text/javascript">
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET", "Library.xml", false);
xmlhttp.send();
xmlDoc = xmlhttp.responseXML;
var f = xmlDoc.getElementsByTagName("FeaturedEntry");
var x = xmlDoc.getElementsByTagName("BookEntry");
for (b = 0; b < x.length; b++) {
var bToString = b.toString();
var overviewOpener = "#overview" + bToString;
var previewDialog = "#preview" + bToString;
// increase the default animation speed to exaggerate the effect
$.fx.speeds._default = 1000;
$(function () {
$(previewDialog).dialog({
autoOpen : false,
show : "blind",
hide : "explode"
});
$(overviewOpener).click(function () {
$(previewDialog).dialog("open");
return false;
});
});
document.write(overviewOpener + " " + b.value + " " + bToString);
document.write("<div id='overview");
document.write(b);
document.write("'><img id='cover' src='/ClientBin/");
icon = x[b].getAttribute("Icon");
document.write(icon);
document.write("'/><br><strong><a href='http://www.google.com'>");
title = x[b].getAttribute("Title");
document.write(title);
document.write("</a></strong><br>");
author = x[b].getAttribute("Author");
document.write(author);
document.write("<br>");
document.write("Rating:");
rating = x[b].getAttribute("Rating");
numStars = Math.round(rating * 5);
for (n = 0; n < numStars; n++) {
document.write("<img src='/Media/star.png'/>");
}
document.write("<br><a><img id='button' src='Media/Small_iBookstore_Badge.gif' /></a>");
document.write("</div>");
document.write("<div id='preview");
document.write(b);
document.write("'>This is a test ");
document.write(b);
document.write("</div>");
}
</script>
关闭我的朋友。闭 包。问题是$(function () {
不像你想象的那样内联执行,它是在 DOM 加载时执行的。但是你的var b
在$(function)
调用之外,这意味着当调用这个函数时,它将拾取当前版本的b,这就是为什么你看到它只适用于最后一个。你需要把你的 $(函数)完全放在你的 for 循环之外,然后做这样的事情:
$(function () {
//add the for loop here
for (var b = 0; b < x.length; b++) {
$("#preview" + b).dialog({
autoOpen: false,
show: "blind",
hide: "explode"
});
$("#overview" + b).click(function () {
$("#preview" + b).dialog("open");
return false;
});
});
话虽如此。你仍然以错误的方式做这件事。像这样的循环也不是正确的方法。你真正应该做的是而不是这个:
document.write("<div id='preview");
document.write(b);
document.write("'>This is a test ");
document.write(b);
document.write("</div>");
这样做:
document.write("<div class='preview'>This is a test ");
document.write(b);
document.write("</div>");
看看我们如何为每个div提供相同的"预览"类?
现在你可以这样做:
$(function () {
//no for loop needed now
$(".preview").dialog({
autoOpen: false,
show: "blind",
hide: "explode"
});
编辑:你是对的,你确实需要为每个div提供一个唯一的名称,但不完全是出于你认为的原因。原因是因为jQuery UI对话框将元素移动到DOM的顶部。 最初,我以为你会做这样的事情:
$(".overview").closest(".SomeContainerDiv").find(".preview")
....但不幸的是,在这种情况下(因为div 被移动了),你不能这样做。看看这个jsfiddle:
http://jsfiddle.net/ehs4K/
我无法浏览整个代码,但据我所知,概述div 是动态创建的。对于动态创建的元素,您必须使用
$(overviewOpener).live("click",function(){
//hanfle click function});
相关文章:
- Windows8应用程序(html&Javascript):从图片库(除了文件选择器)显示图像的另一种方式
- 在动态创建的元素上获取对特定选择器的引用
- AngularJS-在JSON选择器中使用变量名
- 每当您在选择器内移动鼠标时,悬停功能就会重复
- 使用CSS或js,使用动态选择器选择任意li的下一个元素
- jQuery最近父级的数据属性选择器
- 为什么不'在JQuery中找到第二个css选择器的工作
- 为什么 .focus() 不起作用,而 .css(“color”,“red”) 在同一个选择器上起作用
- jquery IE7中的第n个选择器
- Webdriver.io pageObject模式-通过传递参数来定义元素选择器
- jquery日期选择器年份范围默认值
- jquery日期选择器失去了交互性
- JQuery:如何在 jquery 选择器中使用 for 循环变量
- 未定义的 ajax post with for 循环和字符串选择器
- 在 jQuery 中,我怎样才能正确地将选择器用作 for 循环中的变量
- OneDrive for Business是否有客户端javascript库提供文件/文件夹选择器控件?
- 如何在for循环中使用jquery选择器
- 为什么在这段代码中for循环在Jquery选择器之前执行?
- For循环对图像选择器不起作用
- jQuery标签'for'属性选择器