Javascript jQuery$。每个都有多个div名称
你好,下面的问题, 我需要一个循环中每个div的id属性字符串,我如何才能做到这一点Javascript jQuery$。每个都有多个div名称,javascript,jquery,Javascript,Jquery,你好,下面的问题, 我需要一个循环中每个div的id属性字符串,我如何才能做到这一点 <div id="3r23r32_ProgressBar" class="upload-progress-bar"></div> <div id="gfdgfdgfd_ProgressBar" class="upload-progress-bar"></div> 将获取当前元素的id属性文本 正确的.each()示例 $("div.upload-progress
<div id="3r23r32_ProgressBar" class="upload-progress-bar"></div>
<div id="gfdgfdgfd_ProgressBar" class="upload-progress-bar"></div>
将获取当前元素的id属性文本
正确的.each()
示例
$("div.upload-progress-bar").each(function (index,value) {
alert(this.id);
});
请参阅以获得可能有助于您的用例的答案
将获取当前元素的id属性文本
正确的.each()
示例
$("div.upload-progress-bar").each(function (index,value) {
alert(this.id);
});
请参阅以获取可能对您的用例有帮助的答案。尝试以下方法:
$("div.upload-progress-bar").each(function (index,value) {
alert($(this).attr("id"));
$('#' + value + 'ProgressBar').animate({
'width': 10 + '%'
}, 250).animate({
'width': 25 + '%'
}, 250).animate({
'width': 65 + '%'
}, 250).animate({
'width': 95 + '%'
}, 250).animate({
'width': 100 + '%'
}, 250);
});
试试这个:
$("div.upload-progress-bar").each(function (index,value) {
alert($(this).attr("id"));
$('#' + value + 'ProgressBar').animate({
'width': 10 + '%'
}, 250).animate({
'width': 25 + '%'
}, 250).animate({
'width': 65 + '%'
}, 250).animate({
'width': 95 + '%'
}, 250).animate({
'width': 100 + '%'
}, 250);
});
在回调函数中,
此
引用正在循环的每个元素,因此您根本不需要ID来设置元素的动画。只需调用$(this)。设置动画(
但是为了通过div元素进行迭代,您必须使用
$(“div.upload-progress-bar”)。每个(函数(在回调函数中,这个
指的是您正在循环的每个元素,所以您根本不需要ID来设置元素的动画。只需调用$(this)。animate(
但是为了通过div元素进行迭代,必须使用$(“div.upload-progress-bar”)。每个(函数(
都需要$(…)。每个不是$。每个
在我看来,你根本不需要id
——使用this
$("div.upload-progress-bar").each(function (index,value) {
//use `this` and `this.id` if you _really_ need the id
$(this).animate(
//...
)
});
您想要$(…)。每个不是$。每个
在我看来,你根本不需要id
——使用this
$("div.upload-progress-bar").each(function (index,value) {
//use `this` and `this.id` if you _really_ need the id
$(this).animate(
//...
)
});
试试这个
$("div.upload-progress-bar").each(function (index,elem) {
var value = $(elem).attr("id"); // try elem.id, i guess that would work too
$('#' + value).animate({ // you must remove ProgressBar it is included in the value variable
'width': 10 + '%'
}, 250).animate({
'width': 25 + '%'
}, 250).animate({
'width': 65 + '%'
}, 250).animate({
'width': 95 + '%'
}, 250).animate({
'width': 100 + '%'
}, 250);
});
试试这个
$("div.upload-progress-bar").each(function (index,elem) {
var value = $(elem).attr("id"); // try elem.id, i guess that would work too
$('#' + value).animate({ // you must remove ProgressBar it is included in the value variable
'width': 10 + '%'
}, 250).animate({
'width': 25 + '%'
}, 250).animate({
'width': 65 + '%'
}, 250).animate({
'width': 95 + '%'
}, 250).animate({
'width': 100 + '%'
}, 250);
});
我忘记了DIV请复习我的问题谢谢:)@SaschaHeim我做了,我的答案会做你想做的。SaschaHeim是对的——这会做你想做的。@Darhazer的答案确实是你想做的,而不是重建选择器字符串。在这两种情况下,请注意关键字this
的存在。这在JavaScript中是一个关键概念,无论你是否使用库不管你喜不喜欢jQuery,我忘记了div,请回顾我的问题谢谢:)@SaschaHeim我做了,我的答案会做你想做的。SaschaHeim是对的——这会做你想做的。@Darhazer的答案确实是你想做的,而不是重建选择器字符串。在这两种情况下,请注意关键字this
的存在。这在JavaScript中是一个关键概念,无论你是否使用库你喜欢还是不喜欢jQuery。$。each()
不是那样使用的…你确定你不是指$(sel)。each()
而是?@32bitkid-$。each()可以那样使用;)。为什么不直接调用。在$('div.upload-progress-bar'上设置动画呢
selector…?@MatthewPatrickCashatt:要迭代字符串中的字符吗?我想它可能在某些浏览器中工作,但它不会执行OP试图执行的操作。$。each()
不是那样使用的…你确定你不是指$(sel)。each()
而是指@32bitchid-$。each()可以这样使用;)。为什么不直接在$('div.upload-progress-bar')上调用.animate
selector…?@MatthewPatrickCashatt:迭代字符串中的字符?我想它可能在某些浏览器中工作,但它不会执行OP试图执行的操作。完全不需要在这里使用jQuery…this.id
完全可以胜任这项工作。是的,当您通过jQuery介绍JS时会发生这种情况。否这不是一件坏事——这是我学习的方式。但在某一点上,你需要开始学习纯JS。-1你犯了同样的错误。OP是。执行$。每个(“div.upload-progress-bar”,
迭代字符串中的字符(在允许它的浏览器中)。它不进行DOM选择。@jondavidjohn-错误。$(此)构造一个jQuery对象,这样他就可以对它调用jQuery方法,而“this”指向一个DOM对象使用jQuery是一种不好的做法。@amnotiam噢,天哪,我明白你的意思了。我只是复制了OP的代码。是的,你是对的,那是一个字符串而不是选择器。我的不好——我更关注id部分。在这里完全不必要地使用jQuery…这个.id
完全适合这份工作。是的,这就是hap当你通过jQuery对JS进行介绍时,这是一件不错的事情——这是我学习的方式。但在某一点上,你需要开始学习纯JS。-1你犯了同样的错误。操作$。每个(“div.upload-progress-bar”
都会迭代字符串中的字符(在允许的浏览器中)。它不进行DOM选择。@jondavidjohn-错误。$(this)构造了一个jQuery对象,这样他就可以对它调用jQuery方法,而“this”指向一个DOM对象。“this”当使用jQuery时,这是一种不好的做法。@amnotiam哦,天哪——我知道你现在在说什么了。我刚刚复制了OP的代码。是的,你是对的,这是一个字符串,而不是选择器。我的不好——我更关注id部分。jQuery对象没有id
属性。是的,我甚至从来没有看到过这就是我在注释中编写的原因。哇,还有基本上相同的答案我得到了3张反对票:)。回答老年退休金计划的实际问题得到了+1票。@MatthewPatrickCashatt:如果你认为这是相同的答案,那么你就不知道$(string).each()
和$each(string)之间的区别了
。正如@amnotiam所指出的,我匆忙地将OP的代码复制了一遍,没有注意到他用字符串代替了选择器。有了这个小小的改变,它工作得很好:。OP只是询问如何获取div的id。只需说“…;”)jquery对象没有id
属性。是的,我甚至从来没有看到过这就是为什么我写评论。哇,我得到了3个基本相同答案的反对票:)。+1回答OPs的实际问题。@MatthewPatrickCashatt:如果你认为这是相同的答案,那么你不知道$(字符串)之间的区别。每个()
和$。每个(字符串)
。正如@amnotiam所指出的,