admin管理员组文章数量:1279044
I am working on showing a message when a particular element has a width of less than 50, however there are multiple elements with the same class and only the first element in the page is displaying the message. here is the jsfiddle /
Here is the function.
function checkads() {
if ($('#container').height() < 50) {
$('#container').parent().parent().prepend('<div id="ad-notice">Please support our website</div>');
//
}
}
$(document).ready(checkads);
My question is, how do you make the message prepend to all element id instances found. I have various advertisements around my website, they are all wrapped in an div element named advertisement_container
then how do I match them all at once
I am working on showing a message when a particular element has a width of less than 50, however there are multiple elements with the same class and only the first element in the page is displaying the message. here is the jsfiddle http://jsfiddle/MaNdn/23/
Here is the function.
function checkads() {
if ($('#container').height() < 50) {
$('#container').parent().parent().prepend('<div id="ad-notice">Please support our website</div>');
//
}
}
$(document).ready(checkads);
My question is, how do you make the message prepend to all element id instances found. I have various advertisements around my website, they are all wrapped in an div element named advertisement_container
then how do I match them all at once
- 2 IDs are unique. Please do not use the same ID for multiple elements. – Benjamin Gruenbaum Commented Jul 6, 2013 at 0:34
-
I have various advertisements around my website, they are all wrapped in an div element named
advertisement_container
then how do I match them all at once? – David Garcia Commented Jul 6, 2013 at 0:38 - 2 @JoseDavidGarciaLlanos use class instead – Dom Commented Jul 6, 2013 at 0:39
2 Answers
Reset to default 7You need to use each() to iterate through the matched element. Instead of using same id for multiple elements use the same class as the id of element is supposed to be unique. To select multiple elements with same class you can use attribute selector like $('[id=container]') but it is better to use class and keep the ids of elements unique.
Live Demo
function checkads() {
$('.someclass').each(function(){
if($(this).height() < 50) {
$(this).parent().parent().prepend('<div id="ad-notice">Please support our website</div>');
}
});
}
$(document).ready(checkads);
ID has to be unique. Change it to be a class, not an ID, then you can use filter and something like
function checkads() {
$('.container').filter(function (index) {
return $(this).height() < 50;
}).parent().parent().prepend('<div id="ad-notice">Please support our website</div>');
}
本文标签: javascriptJquery matching multiple elements with same idclassStack Overflow
版权声明:本文标题:javascript - Jquery matching multiple elements with same idclass - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741270936a2369282.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论