Вопрос

Question. I've tried to add this old jQuery Regex Selector plugin but it doesn't seem to give me back the right tag. At least, it says undefined when I try to return any info on it. I hope to use this someday for a game so I can select dynamic tags and things like all the foreground elements.

$('#button').click(function() {
    var elm = $('img:regex(data:extension, png)');
    elm.each(function() { alert($(this).attr('alt')); });
});

But all I get back is undefined when I alert the elm I get back [object Object]

JSFiddle

Это было полезно?

Решение

From the documentation you linked to: (emphasis mine)

Additionally it allows you to query data strings added to elements via jQuery’s ‘data’ method:

// Add data property to all images (just an example);
$('img').each(function(){
    $(this).data('extension', $(this)[0].src.match(/\.(.{1,4})$/)[1]);
});

// Select all images with PNG or JPG extensions:
$('img:regex(data:extension, png|jpg)');

You would need to run both parts of that code, not just the second part. Your image in your example does not have a data attribute.

--EDIT--

Also, you don't need regex to get this functionality, just do this:

var elm = $('img[src$=png]');

Here's a fiddle

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top