te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>html - JavaScript "onClick" on li elements - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - JavaScript "onClick" on li elements - Stack Overflow

programmeradmin3浏览0评论

I made a menu bar with ul and li and I want some onClick events linked to JavaScript functions.

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.

Here is the 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:

#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:

function showAbout2()
{
    console.log("bonjour");
}

ANSWER:

The answer was :

Removing the z-index.


I wanted to click on a lower-layered element. So i'll try this /

Thanks everyone : )

I made a menu bar with ul and li and I want some onClick events linked to JavaScript functions.

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.

Here is the 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:

#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:

function showAbout2()
{
    console.log("bonjour");
}

ANSWER:

The answer was :

Removing the z-index.


I wanted to click on a lower-layered element. So i'll try this http://www.vinylfox./forwarding-mouse-events-through-layers/

Thanks everyone : )

Share Improve this question edited Jul 3, 2017 at 13:04 freginold 3,9563 gold badges15 silver badges29 bronze badges asked Sep 23, 2013 at 12:45 ogdabouogdabou 5922 gold badges11 silver badges42 bronze badges 4
  • Your markup hasn't been pasted correctly. Can we also see your javascript? – Mister Epic Commented Sep 23, 2013 at 12:47
  • Adding a link to jsfiddle code might help even more :) thanks – Pawcu Commented Sep 23, 2013 at 12:48
  • 5 I think your problem is with the z-index: -1. – Harry Commented Sep 23, 2013 at 12:52
  • Please put the answer into the answer section and accept (either Harry or the original poster). – chryss Commented Sep 23, 2013 at 15:33
Add a ment  | 

4 Answers 4

Reset to default 5

Remove the z-index on the #menuContainer. This is pushing the #menuContainer behind and hence is preventing the click event from happening/taking effect.

#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 ments 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. To overe this, add the below lines to #titleContainer and #mainContent after removing the item mentioned in the original answer

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.

Remove z-index: -1, it's preventing you from clicking the link.

http://jsfiddle/w4cE3/

CSS

#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...

 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 ment.

You have to Remove the z-index on the id tag #menuContainer. This should work

#menuContainer{
background-color: #333333;
line-height: 35px;
position: relative;
text-align: center;
}
发布评论

评论列表(0)

  1. 暂无评论