[英]JavaScript “onClick” on li elements
I made a menu bar with ul
and li
and I want some onClick
events linked to JavaScript functions. 我用ul
和li
制作了一个菜单栏,我想要一些链接到JavaScript函数的onClick
事件。
But it does not work, clicking on menus don't do anything at all. 但这不起作用,单击菜单根本没有任何作用。
The function is on a JS file I already use and I tested it on another element, it seems that id doesn't work only inside of my li
. 该函数在我已经使用的JS文件上,并且我在另一个元素上对其进行了测试,看来id仅在li
内部不起作用。
Here is the HTML: 这是HTML:
<div id="header">
<div id="titleContainer">
blablabl
</div>
<ul id="menuContainer">
<li class="menuItem" id="videoMenu">
<a href="#" onClick="showAbout2();"> Video</a>
</li>
<li class="menuItem" id="playlistMenu">
Playlist
</li>
<li class="menuItem" id="aboutMenu">
About
</li>
<li class="menuItem" id="controlsMenu">
Controls
</li>
</ul>
</div>
The CSS: CSS:
#titleContainer
{
box-shadow: 0px 0px 5px #000;
color: lightgrey;
font-size: 20px;
font-weight: bold;
line-height: 50px;
text-align: center;
}
#menuContainer
{
background-color: #333333;
line-height: 35px;
margin: 0;
position: relative;
text-align: center;
z-index: -1;
}
#menuContainer li
{
display: inline;
list-style: none;
}
The JS: JS:
function showAbout2()
{
console.log("bonjour");
}
ANSWER: 回答:
The answer was : 答案是:
Removing the z-index. 删除z-index。
I wanted to click on a lower-layered element. 我想单击一个较低层的元素。 So i'll try this http://www.vinylfox.com/forwarding-mouse-events-through-layers/ 因此,我将尝试使用此http://www.vinylfox.com/forwarding-mouse-events-through-layers/
Thanks everyone : ) 感谢大家 : )
Remove the z-index
on the #menuContainer
. 删除#menuContainer
上的z-index
。 This is pushing the #menuContainer
behind and hence is preventing the click event from happening/taking effect. 这将#menuContainer
推到后面,因此阻止了click事件的发生/生效。
#menuContainer
{
background-color: #333333;
line-height: 35px;
margin: 0;
position: relative;
text-align: center;
z-index: -1; /*Remove this line.*/
}
function showAbout2() { console.log("bonjour"); }
#titleContainer { box-shadow: 0px 0px 5px #000; color: lightgrey; font-size: 20px; font-weight: bold; line-height: 50px; text-align: center; } #menuContainer { background-color: #333333; line-height: 35px; margin: 0; position: relative; text-align: center; } #menuContainer li { display: inline; list-style: none; }
<div id="header"> <div id="titleContainer">blablabl</div> <ul id="menuContainer"> <li class="menuItem" id="videoMenu"> <a href="#" onClick="showAbout2();"> Video</a> </li> <li class="menuItem" id="playlistMenu">Playlist</li> <li class="menuItem" id="aboutMenu">About</li> <li class="menuItem" id="controlsMenu">Controls</li> </ul> </div>
The below update is not part of the original question but is added based on your comments to the answer. 以下更新不是原始问题的一部分,而是根据您对答案的评论添加的。
It seems like removing the z-index: -1
is breaking the box-shadow
effect on your website because it is bringing the element back to the front. 似乎删除了z-index: -1
打破了您网站上的box-shadow
效果,因为它使元素重新回到了前面。 To overcome this, add the below lines to #titleContainer
and #mainContent
after removing the item mentioned in the original answer 为了解决这个问题,请在删除原始答案中提到的项目后,将以下#titleContainer
行添加到#titleContainer
和#mainContent
中
z-index: 2;
position: relative;
That will get the shadow back because instead of sending #menuContainer
back, we are bringing the other two in front. 这将使阴影重新出现,因为我们没有将#menuContainer
送回,而是将其他两个放在了前面。
Remove z-index: -1
, it's preventing you from clicking the link. 删除z-index: -1
,这可以防止您单击链接。
http://jsfiddle.net/w4cE3/ http://jsfiddle.net/w4cE3/
#menuContainer
{
background-color: #333333;
line-height: 35px;
margin: 0;
position: relative;
text-align: center;
}
All you need to do is loop through the list-item elements and add event listeners... 您需要做的就是遍历list-item元素并添加事件侦听器...
var l = document.getElementById('menuContainer').getElementsByTagName('li');
for (var i=0; i<l.length; i++)
{
l[i].addEventListener('click', function() {alert('add any function in place of this alert!');},false);
}
If you need something more specific please comment. 如果您需要更具体的内容,请发表评论。
You have to Remove the z-index on the id tag #menuContainer. 您必须删除ID标签#menuContainer上的z-index。 This should work 这应该工作
#menuContainer{
background-color: #333333;
line-height: 35px;
position: relative;
text-align: center;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.