Javascript jquery中是否有类似indexof()的方法?或者怎么做?
请看下面的代码:Javascript jquery中是否有类似indexof()的方法?或者怎么做?,javascript,jquery,html,css,Javascript,Jquery,Html,Css,请看下面的代码: $(".head img").hover(function(){ var id = $(this).attr('id'); $(this).attr("src","images/"+id+"_hover.gif"); },function(){ var id = $(this).attr('id'); $(this).attr("src","images/"+id+".gif"); }) 如果我不使用id,只需使用类似的this.indexof
$(".head img").hover(function(){
var id = $(this).attr('id');
$(this).attr("src","images/"+id+"_hover.gif");
},function(){
var id = $(this).attr('id');
$(this).attr("src","images/"+id+".gif");
})
如果我不使用id,只需使用类似的this.indexof。
下面的代码是错误的,但我想让你知道我的意思:
$(".head .fl_r img").hover(function(){
$(this).attr("src","images/"+this.indexof(in array())+"_hover.gif");
},function(){
$(this).attr("src","images/"+this.indexof(in array())+".gif");
})
在jquery中如何实现这一点?indexOf可用于字符串对象 如果您想通过jquery在attr上使用它,您应该尝试以下方法:
$(this).attr("src").indexOf("needle");
更多信息:
indexOf可用于字符串对象 如果您想通过jquery在attr上使用它,您应该尝试以下方法:
$(this).attr("src").indexOf("needle");
更多信息:
我会调查,因为它提供了一个索引
$(".head img").each(function(index, element) {
element.hover(
function() {
$(this).attr("src","images/"+index+"_hover.gif");
},
function() {
$(this).attr("src","images/"+index+".gif");
}
);
});
我会调查,因为它提供了一个索引
$(".head img").each(function(index, element) {
element.hover(
function() {
$(this).attr("src","images/"+index+"_hover.gif");
},
function() {
$(this).attr("src","images/"+index+".gif");
}
);
});
是的,这是一种方法:
如果没有参数传递给.index方法,则返回值是一个整数,指示jQuery对象中第一个元素相对于其同级元素的位置
将其用作$this.attrsrc,images/+$this.index+\u hover.gif 是的,这是一种方法:
如果没有参数传递给.index方法,则返回值是一个整数,指示jQuery对象中第一个元素相对于其同级元素的位置
将其用作$this.attrsrc,images/+$this.index+\u hover.gif 你能试着澄清一下你的构造this.indexOfin array应该做什么吗?你能试着澄清一下你的构造this.indexOfin array应该做什么吗?小心你使用的是哪个版本的jQuery,索引方法在当前版本中被破坏了。谢谢你的帮助。我也检查了index方法,但是它不能正常工作。我不知道为什么:$.head.fl\r img.hoverfunction{number=$this.index+1;$this.attrsrc,images/nav+number+\uhover.gif;},function{number=$this.index+1;$this.attrsrc,images/nav+number+.gif;}@迈克尔:我怀疑缪的链接可能会给你一些线索,说明它为什么不起作用。如果您可以依赖此选项工作,则此选项可能是一个不错的选项。请注意用于此操作的jQuery版本,当前版本中的索引方法已损坏。感谢您的帮助。我也检查了index方法,但是它不能正常工作。我不知道为什么:$.head.fl\r img.hoverfunction{number=$this.index+1;$this.attrsrc,images/nav+number+\uhover.gif;},function{number=$this.index+1;$this.attrsrc,images/nav+number+.gif;}@迈克尔:我怀疑缪的链接可能会给你一些线索,说明它为什么不起作用。如果您可以信赖此选项,则此选项可能是一个不错的选项。感谢您的帮助。我测试了代码,效果很好!$。head.fl_r img.eachfunctionindex,元素{$this.hoverfunction{number=index+1;$this.attrsrc,images/nav+number+_hover.gif;},函数{$this.attrsrc,images/nav+number+.gif;};谢谢你的帮助。我测试了代码,效果很好!$。head.fl_r img.eachfunctionindex,元素{$this.hoverfunction{number=index+1;$this.attrsrc,images/nav+number+_hover.gif;},函数{$this.attrsrc,images/nav+number+.gif;};