使用 jQuery 从代码中指定的元素中选择具有给定类的下一个元素
我现在已经尝试了很多选择!
我有一组包含缩略图的元素,这些缩略图由 ASP 重复创建并通过 AJAX 加载动态呈现到页面,其中有一些图标,单击这些图标会放大图像。
重复的 HTML 看起来像这样:
<div class="prodImgContainer">
<img src="*url*" id="*id*" class="itemImages" />
<div class="imgControl">
<span class="tinyIcons tinyDelete imgDelete" id="delete*id*"></span>
<br />
<span class="tinyIcons tinyZoom imgZoom" id="zoom*id*"></span>
</div>
</div>
因此,当您使用 imgZoom
类进行任何操作时,它都会触发放大。这是通过我用于加载模式窗口的标准函数来完成的。
$('.imgZoom').live('click', function () {
var prodId = $('#productId').val();
var thisImage = ($(this).attr('id').replace('zoom', '')).replace('-tb', '');
var img = $("<img />").addClass('nextImg').attr('id', 'thisImg' + thisImage).attr('src', '*BASE URL *' + prodId + '/' + thisImage + '.jpg').load(function () {
if (!this.complete || typeof this.naturalWidth == "undefined" || this.naturalWidth == 0) {
alert('broken image!');
} else {
$("#largeImgContainer").empty();
$('#showImg').click();
$("#largeImgContainer").css('display', 'none').append(img).delay('1000').fadeIn('slow');
};
});
});
上面的代码将放大的图像加载到现有容器中。
现在...我希望能够通过单击创建的图像来触发下一个 imgZoom
元素上的 click
事件。
不过,调用元素不再在代码中,所以我尝试这样做:
$('.nextImg').live('click', function () {
var nextImage = ($(this).attr('id').replace('thisImg', '')).replace('-tb', '');
$('#zoom' + nextImage + '-tb').next('.nextImg').click();
});
在这里,我能够确定最初单击以打开此图像的元素的 ID,
然后我期待 $ ('#zoom' + nextImage + '-tb').next('.nextImg').click();
查找该元素,然后查找下一个具有 nextImg
并单击它
但是它不起作用...我错过了什么?
I've tried loads of options now!
I have a set of elements which contain thumbnails, created by an ASP Repeated and dynamically rendered to a page by an AJAX load, within them are some icons which when clicked enlarge an image.
The repeated HTML looks like this:
<div class="prodImgContainer">
<img src="*url*" id="*id*" class="itemImages" />
<div class="imgControl">
<span class="tinyIcons tinyDelete imgDelete" id="delete*id*"></span>
<br />
<span class="tinyIcons tinyZoom imgZoom" id="zoom*id*"></span>
</div>
</div>
So, when you anything with the class imgZoom
it fires the enlargement. This is done by a standard function which I have in place for loading modal windows.
$('.imgZoom').live('click', function () {
var prodId = $('#productId').val();
var thisImage = ($(this).attr('id').replace('zoom', '')).replace('-tb', '');
var img = $("<img />").addClass('nextImg').attr('id', 'thisImg' + thisImage).attr('src', '*BASE URL *' + prodId + '/' + thisImage + '.jpg').load(function () {
if (!this.complete || typeof this.naturalWidth == "undefined" || this.naturalWidth == 0) {
alert('broken image!');
} else {
$("#largeImgContainer").empty();
$('#showImg').click();
$("#largeImgContainer").css('display', 'none').append(img).delay('1000').fadeIn('slow');
};
});
});
The above code loads the enlarged image into an existing container.
Now... I want to be able to trigger a click
event on the next imgZoom
element by clicking the image which is created.
The calling element is no longer in the code though, so I am trying to do it like this:
$('.nextImg').live('click', function () {
var nextImage = ($(this).attr('id').replace('thisImg', '')).replace('-tb', '');
$('#zoom' + nextImage + '-tb').next('.nextImg').click();
});
Here, I am able to determine the ID of the element which was originally clicked to open up this image
I am then expecting $('#zoom' + nextImage + '-tb').next('.nextImg').click();
to find that element, then find the next one with the class nextImg
and click it
But it doesnt work... what am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您应该能够将单击附加到动态创建的图像元素,以触发对下一个 imgZoom 元素的单击,即类似:
You should be able to attach the click to the dynamically created image element to trigger a click on the next imgZoom element, i.e. something like:
我自己也犯过同样的错误,但是 .next() 不能那样工作。它的作用是仅当 DOM 中当前元素与选择器匹配时才返回紧随其后的元素,否则不返回任何元素。
如果您想要 DOM 中当前选定元素之后的第一个元素与特定选择器匹配,您可以使用如下内容:
I've made the same mistake myself, but .next() doesn't work that way. What it does is return the element that is immediately after the current element in the DOM only if it matches the selector, otherwise it returns no elements.
If you want the first element that comes after the currently selected element in the DOM that matches a specific selector, you could use something like this:
next()
仅返回 DOM 中的下一个元素(紧随其后的元素),您可以从文档中获取
next()
only returns the next element in the DOM (the elemement that is immediatly after it), you could doTaken from the docs
很难理解你想要什么,你的文字和代码之间存在差异。
也许这一行
会起作用:
我不明白的是:“-tb”的用法 - 它在哪里是 id 的一部分,哪里不是?为什么调用 .next('.nextImg')?我以为您想单击下一个 imgZoom 元素。在您的代码中,只有 largeImgContainer 中的(单个)图像具有 nextImg 类。
It's hard to understand what you want, there are differences between your words and your code.
Maybe instead this line
this would work:
What I did not understand: the usage of '-tb' - where is it part of the id and where not? Why did you call .next('.nextImg')? I thought you wanted to click the next imgZoom element. In your code only the (single) image in largeImgContainer has the class nextImg.
啊啊!!
以上均无效,但感谢您的帮助。
我最终创建了一个图像数组并传递了位置,以便下一个图像单击将在数组中的下一个位置打开文件。
Argh!!
None of the above worked, but thanks for the help.
I ended up creating an array of images and passing the position through so the next image click would open the file in the next position in the array.