jQuery";每一个;方法
我是javascript初学者,在使用jQuery的“each”方法时遇到了一些问题。我在Joomla网站上有一个画廊模块,可以作为一个简单的图像滑块使用。我添加了一些代码,允许在链接的图像上使用lightbox。我现在想做的是将缩略图标题(模块当前添加的标题)复制到链接中的Title属性中,使其显示在每个图像的lightbox中 下面是HTML的结构jQuery";每一个;方法,jquery,loops,each,Jquery,Loops,Each,我是javascript初学者,在使用jQuery的“each”方法时遇到了一些问题。我在Joomla网站上有一个画廊模块,可以作为一个简单的图像滑块使用。我添加了一些代码,允许在链接的图像上使用lightbox。我现在想做的是将缩略图标题(模块当前添加的标题)复制到链接中的Title属性中,使其显示在每个图像的lightbox中 下面是HTML的结构 <div class="camera_wrap" id="camera_wrap_106"> <div class="came
<div class="camera_wrap" id="camera_wrap_106">
<div class="cameraContents">
<div class="cameraContent">
<a class="camera_link" href="lightbox-large.jpg" title="This is where i need the caption"></a>
<div class="camera_caption">
<div>This is the caption</div>
</div>
</div>
<div class="cameraContent">
<a class="camera_link" href="lightbox-large.jpg" title="This is where i need the caption"></a>
<div class="camera_caption">
<div>This is the caption</div>
</div>
</div>
<div class="cameraContent">
<a class="camera_link" href="lightbox-large.jpg" title="This is where i need the caption"></a>
<div class="camera_caption">
<div>This is the caption</div>
</div>
</div>
</div>
提前谢谢 您应该使用此
:
$("#camera_wrap_106 .cameraContent").each(function() {
$(this).attr('title', $(this).html());
});
。每个
在迭代中提供元素作为函数的上下文(即此
)
它还作为第二个参数提供(第一个参数是索引),这意味着您也可以编写以下内容:
$("#camera_wrap_106 .cameraContent").each(function(index, element) {
$(element).attr('title', $(element).html());
});
您应该使用此
:
$("#camera_wrap_106 .cameraContent").each(function() {
$(this).attr('title', $(this).html());
});
。每个
在迭代中提供元素作为函数的上下文(即此
)
它还作为第二个参数提供(第一个参数是索引),这意味着您也可以编写以下内容:
$("#camera_wrap_106 .cameraContent").each(function(index, element) {
$(element).attr('title', $(element).html());
});
您可以使用的回调函数 或者,如果您想使用each语句,只需在选择器中设置
$("#camera_wrap_106 .cameraContent").each(function() {
$("a.camera_link",this).attr('title', $(".camera_caption div",this).text());
});
在选择器中添加上下文与使用find是一样的
$('element',context) == $(context).find('element')
您可以使用的回调函数 或者,如果您想使用each语句,只需在选择器中设置
$("#camera_wrap_106 .cameraContent").each(function() {
$("a.camera_link",this).attr('title', $(".camera_caption div",this).text());
});
在选择器中添加上下文与使用find是一样的
$('element',context) == $(context).find('element')
或
$(“#camera_wrap_106.cameraContent”).attr(“title”,function(){return$(this.html();})代码>@Tomalak对。即使它不能解释。在这种情况下,它显然是更清洁的解决方案。谢谢你的建议。然而,给出的代码也在做同样的事情。出于某种原因,它仍在将第一个标题div复制到每个标题属性。您确定吗?我不明白这怎么可能。你没有缓存问题吗。或者$(“#camera_wrap_106.cameraContent”).attr(“title”,function(){return$(this.html();})代码>@Tomalak对。即使它不能解释。在这种情况下,它显然是更清洁的解决方案。谢谢你的建议。然而,给出的代码也在做同样的事情。出于某种原因,它仍在将第一个标题div复制到每个标题属性。您确定吗?我不明白这怎么可能。你没有缓存问题吗。