I have a d开发者_JS百科ifferent pages with same header written with php. My problem is : when there is some ID for a div for example in two different page while I am trying to do different processing with Jquery for the two divs. How can I organize Jquery code to avoid this conflict ?
You could add a class to the different DIVs to target them differently in jQuery. Like the following.
$('#myid.cow').show();
$('#myid.bull').fade();
While you're HTML will look like this
<div id="myid" class="cow">Mooo</div>
and on the other page.
<div id="myid" class="bull">Snoort</div>
Another possible solution, is to pull the javascript out of a central file and just include it in the <head>
of the current page.
Either way works.
Don'T know exactly if I am getting your question right, but I think you could assign a different class or id for a parent element (e.g. the body) for each page.
Then you could address them in jQuery with something like this:
$(#page1 div#divid)
and
$(#page2 div#divid)
To be quite honest, I think you should add functionality to your php to be able to include scripts on a per page basis. You don't want to include your ENTIRE javascript for your whole site on every page, do you? If that's not an option, then go with one of the other answers.
精彩评论