admin管理员组文章数量:1279210
I am developing a mobile site and want to use JS for nothing more than adding and removing classes. So, in the interest of keeping things nice and light I don't want to use jQuery.
I have the following HTML:
<div id="masthead">
<a href="index.html" title="Home" id="brand">Brand</a>
<a href="#" id="openPrimaryNav">Menu</a>
<ul id="primaryNav" class="">
<li><a href="index.html" title="Home">Home</a></li>
<li><a href="benefits.html" title="Benefits">Benefits</a></li>
<li><a href="features.html" title="Features">Features</a></li>
<li><a href="casestudies.html" title="Case Studies">Case Studies</a></li>
<li><a href="instore.html" title="In Store">In-Store</a></li>
<li><a href="contact.html" title="Contact">Contact Us</a></li>
<li id="closePrimaryNav"><a href="#" title="Contact">Close Menu</a></li>
</ul>
</div>
and the following JS so far:
window.onLoad = init;
function init()
{
document.getElementById('openPrimaryNav').onClick = openPrimaryNav();
document.getElementById('closePrimaryNav').onClick = closePrimaryNav();
}
function openPrimaryNav()
{
document.getElementById('primaryNav').className = 'open';
}
function closePrimaryNav()
{
document.getElementById('primaryNav').className = '';
}
I cannot get this working can anyone tell me what I am doing wrong? Many thanks in advance.
CORRECT JS BASED ON ANSWER PROVIDED BELOW:
window.onload = init;
function init()
{
document.getElementById('openPrimaryNav').onclick = openPrimaryNav;
document.getElementById('closePrimaryNav').onclick = closePrimaryNav;
}
function openPrimaryNav()
{
document.getElementById('primaryNav').setAttribute('class','open');
}
function closePrimaryNav()
{
document.getElementById('primaryNav').setAttribute('class','');
}
I am developing a mobile site and want to use JS for nothing more than adding and removing classes. So, in the interest of keeping things nice and light I don't want to use jQuery.
I have the following HTML:
<div id="masthead">
<a href="index.html" title="Home" id="brand">Brand</a>
<a href="#" id="openPrimaryNav">Menu</a>
<ul id="primaryNav" class="">
<li><a href="index.html" title="Home">Home</a></li>
<li><a href="benefits.html" title="Benefits">Benefits</a></li>
<li><a href="features.html" title="Features">Features</a></li>
<li><a href="casestudies.html" title="Case Studies">Case Studies</a></li>
<li><a href="instore.html" title="In Store">In-Store</a></li>
<li><a href="contact.html" title="Contact">Contact Us</a></li>
<li id="closePrimaryNav"><a href="#" title="Contact">Close Menu</a></li>
</ul>
</div>
and the following JS so far:
window.onLoad = init;
function init()
{
document.getElementById('openPrimaryNav').onClick = openPrimaryNav();
document.getElementById('closePrimaryNav').onClick = closePrimaryNav();
}
function openPrimaryNav()
{
document.getElementById('primaryNav').className = 'open';
}
function closePrimaryNav()
{
document.getElementById('primaryNav').className = '';
}
I cannot get this working can anyone tell me what I am doing wrong? Many thanks in advance.
CORRECT JS BASED ON ANSWER PROVIDED BELOW:
window.onload = init;
function init()
{
document.getElementById('openPrimaryNav').onclick = openPrimaryNav;
document.getElementById('closePrimaryNav').onclick = closePrimaryNav;
}
function openPrimaryNav()
{
document.getElementById('primaryNav').setAttribute('class','open');
}
function closePrimaryNav()
{
document.getElementById('primaryNav').setAttribute('class','');
}
Share
Improve this question
edited Mar 18, 2012 at 11:19
mtwallet
asked Mar 18, 2012 at 10:54
mtwalletmtwallet
5,09614 gold badges53 silver badges74 bronze badges
1
- I think this question was already answer here stackoverflow./questions/5169017/… – remi dupuis Commented Mar 18, 2012 at 10:59
2 Answers
Reset to default 7You can use setAttribute
.
window.onload = init;
function init()
{
document.getElementById('openPrimaryNav').onclick = openPrimaryNav;
document.getElementById('closePrimaryNav').onclick = closePrimaryNav;
}
function openPrimaryNav()
{
document.getElementById('primaryNav').setAttribute('class','open');
}
function closePrimaryNav()
{
document.getElementById('primaryNav').setAttribute('class','');
}
It's .onclick
, not .onClick
本文标签: AddRemove class onclick with JavaScript no librarysStack Overflow
版权声明:本文标题:AddRemove class onclick with JavaScript no librarys - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741239647a2363719.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论