jQuery search ID for certain text
I am looking for a way to search for an ID on the current clicked element.
Example:
$('.t_element').click(function(){
if(!$('.t_element [id*=meta]')) {
transModal($(this));
translateText();
}
});
I basically need to say if the clicked element id does not contain the word "meta" then continue with the script, but this is not working.
Sorry if this is confusing.
Thanks! Dennis
Working Example:
if (!$(this).is('[id*=meta]')) {开发者_StackOverflow
transModal($(this));
translateText();
}
If the IDs of the elements aren't going to change, then I'd just include the test in the selector so you're not assigning unneeded .click()
handlers.
$('.t_element:not([id*=meta])').click(function(){
transModal($(this));
translateText();
});
This uses the :not()
selector along with the attribute contains selector to prevent the handler from being assigned to elements where the ID contains meta
.
Sarfraz's version should work. Another method is using .is()
:
if ($(this).is('[id*=meta]')) {
transModal($(this));
translateText();
}
Or, as Patrick says, if you want not to act if the element has an id containing "meta":
if ($(this).not('[id*=meta]')) {
transModal($(this));
translateText();
}
Try with length
like this:
$('.t_element').click(function(){
if($('[id*="meta"]', $(this)).length === 0) {
transModal($(this));
translateText();
}
});
Or:
$('.t_element').click(function(){
if($(this).attr('id').indexOf('meta') <= -1) {
transModal($(this));
translateText();
}
});
精彩评论