简体   繁体   English

从jQuery代码中指定的元素中选择具有给定类的下一个元素

[英]Selecting the next element with a given class from an element specified in code with jQuery

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. 我有一组包含缩略图的元素,这些元素由ASP重复创建,并通过AJAX加载动态呈现到页面上,其中包含一些图标,单击这些图标可以放大图像。

The repeated HTML looks like this: 重复的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>

So, when you anything with the class imgZoom it fires the enlargement. 因此,当您使用imgZoom类进行任何操作时, imgZoom触发放大。 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. 现在...我希望能够通过单击创建的图像来触发下一个imgZoom元素的click事件。

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 在这里,我可以确定最初单击以打开此图像的元素的ID

I am then expecting $('#zoom' + nextImage + '-tb').next('.nextImg').click(); 然后我期望$('#zoom' + nextImage + '-tb').next('.nextImg').click(); to find that element, then find the next one with the class nextImg and click it 找到该元素,然后找到具有nextImg类的下一个元素,然后单击它

But it doesnt work... what am I missing? 但这不起作用...我想念什么?

next() only returns the next element in the DOM (the elemement that is immediatly after it), you could do next()仅返回DOM中的下一个元素(紧随其后的元素),您可以执行

  //this gets the DOM element
  var nextImg = $('#zoom' + nextImage + '-tb').nextAll('.nextImg')[0];
  //wrap this in jQuery and click it
  $(nextImg).click();

Taken from the docs 取自文档

Given a jQuery object that represents a set of DOM elements, the .next() method allows us to search through the immediately following sibling of these elements in the DOM tree and construct a new jQuery object from the matching elements. 给定一个表示一组DOM元素的jQuery对象,.next()方法允许我们在DOM树中搜索这些元素的紧随其后的同级结构,并从匹配的元素构造一个新的jQuery对象。

The method optionally accepts a selector expression of the same type that we can pass to the $() function. 该方法可选地接受与我们可以传递给$()函数的类型相同的选择器表达式。 If the immediately following sibling matches the selector, it remains in the newly constructed jQuery object; 如果紧随其后的同级匹配选择器,则它将保留在新构造的jQuery对象中; otherwise, it is excluded. 否则,将其排除在外。

You should be able to attach the click to the dynamically created image element to trigger a click on the next imgZoom element, ie something like: 您应该能够将点击附加到动态创建的图片元素上,从而触发对下一个imgZoom元素的点击,例如:

...
$("#largeImgContainer").empty();
$('#showImg').click();
$("#largeImgContainer").css('display', 'none').append(img).delay('1000').fadeIn('slow');

// here 'this' refers to the imgZoom element that was clicked
var nextElement = $(this).next('.imgZoom'); 
img.click(function() {
    nextElement.click(); // trigger the click
});
...

I've made the same mistake myself, but .next() doesn't work that way. 我自己也犯了同样的错误,但是.next()不能那样工作。 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. 它所做的是仅当它与选择器匹配时才返回DOM中当前元素之后的元素,否则不返回任何元素。

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: 如果要让第一个元素位于DOM中与特定选择器匹配的当前选定元素之后,则可以使用以下内容:

$('#zoom' + nextImage + '-tb').nextAll('.nextImg:first').click();

It's hard to understand what you want, there are differences between your words and your code. 很难理解您想要什么,您的文字和代码之间存在差异。

Maybe instead this line 也许这行

$('#zoom' + nextImage + '-tb').next('.nextImg').click();

this would work: 这将工作:

$('#zoom' + nextImage + '-tb').parents('.prodImgContainer').next('.prodImgContainer').find('.imgZoom').click();

What I did not understand: the usage of '-tb' - where is it part of the id and where not? 我不明白的是:“-tb”的用法-它在id的什么地方,在哪里呢? Why did you call .next('.nextImg')? 为什么调用.next('。nextImg')? I thought you wanted to click the next imgZoom element. 我以为您想单击下一个imgZoom元素。 In your code only the (single) image in largeImgContainer has the class nextImg. 在您的代码中,只有largeImgContainer中的(单个)图像具有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. 我最终创建了一个图像数组,并通过该位置,以便单击下一个图像将在该数组中的下一个位置打开文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM