English 中文(简体)
Jquery toggling each and every div with same id
原标题:

I want to toggle a bunch of divs that each have the same id. My current jquery code only toggles one div. (I ve tried using a mixture of .next and .parent.next, but haven t found anything yet)

$( #body +id).toggle( 400 );

So: There would be multiple divs with a #body2010 id. I want to toggle all of those.

最佳回答

Duplicate ID names are invalid. You should convert them to classes. jQuery will only match the first matching element for ID tags whereas it would return an array of all class matches. You would then do:

$( .body +id).toggle();
问题回答

You shouldn t have more than 1 element with the same ID in a document - use the class attribute instead.

ID s should be unique. JQuery knows that, so when you re searching for an element with a specific ID, jQuery will only return one element.

from jQuery docs:

#id
Matches a single element with the given id attribute.

http://docs.jquery.com/Selectors/id#id

You should switch the body2010 id to a class attribute.

Knowing nothing about jQuery, I believe an ID should be unique and if you have multiple divs with the same ID, you should be using a class.

ID, as you know, stands for identifier . IDs are unique in all contexts.

You should use classes for this type of DOM query.

jQuery will toggle only the first of those elements since ID s are supposed to be unique on a page. You will get a better result if you assign a class name to each of those divs and use that to toggle.

$( .body  + id).toggle( 400 );




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签