jquery: h1 for body id="home", h4 for every other body id
Howdi all, I'm using a PHP include for my header section and I want to be able to have my logo wrapped in a h1 for the 开发者_运维知识库homepage, and a h4 for the rest of the pages. My homepage has a body ID of "home" and the rest of the pages have their own IDs as well. Is there an easy way to achieve this with jQuery? Thanks in advance.
If your logo element has ID #logo
.
To wrap on your home page, use a normal ID selector, and to wrap on every other page use a "attribute not equals selector":
$('body#home #logo').wrap('<h1>');
$('body[id!="home"] #logo').wrap('<h4>');
That said, the code that's producing your pages should be putting the right tags in the page in the first place!
Seems you should be using a class on those H1/H4s, but if you must, try this:
$('body#home h1').css("background-image","url('/path/to/large/image.png')");
$('body[id!='home'] h4').css("background-image","url('/path/to/small/image.png')");
But I would suggest doing something like this instead:
<body id="home">
<h1 class='logo'></h1>
</body>
<body id="another_page">
<h4 class='logo'></h4>
</body>
CSS:
h1.logo { background-image: url('/path/to/large/image.png'); }
h4.logo { background-image: url('/path/to/small/image.png'); }
精彩评论