15-08-02 例子:下拉菜单(阻止事件冒泡函数)

做一个下拉菜单,当点击表单元素时显示下拉菜单,鼠标放到菜单元素上时改变元素背景色,将菜单中选中的值传到表单中,当点击表单及下拉菜单时外的其他地方时,下拉菜单消失。

<title>下拉菜单</title>
<style type="text/css">
*
{
    margin:0px;
    padding:0px;
}
#pingmu
{
    width:100%;
    height:100%;
    position:fixed;
    background-color:#FF9;
}
#minzu
{
    width:200px;
    height:300px;
    border:1px solid #000;
    text-align:center;
    background-color:#FFF;
}
#minzu div
{
    width:200px;
    height:50px;
    font-size:20px;
    line-height:50px;
    vertical-align:middle;
    text-align:center;
}
#menu
{
    width:198px;
    height:50px;
    font-size:20px;
    line-height:50px;
    vertical-align:middle;
    text-align:center;
}
</style>
</head>

<body>
<div id="pingmu" onclick="guan()">
<input type="text" id="menu" value="民族" onclick="xiala()"/>
<div id="minzu" style="display:none">
    <div id="han" onmouseover="caidan(this)" onmouseout="out(this)" onclick="quzhi(this)">汉族</div>
    <div id="hui" onmouseover="caidan(this)" onmouseout="out(this)" onclick="quzhi(this)">回族</div>
    <div id="man" onmouseover="caidan(this)" onmouseout="out(this)" onclick="quzhi(this)">满族</div>
    <div id="chao" onmouseover="caidan(this)" onmouseout="out(this)" onclick="quzhi(this)">朝鲜族</div>
    <div id="miao" onmouseover="caidan(this)" onmouseout="out(this)" onclick="quzhi(this)">苗族</div>
    <div id="zhang" onmouseover="caidan(this)" onmouseout="out(this)" onclick="quzhi(this)">藏族</div>
</div>
</div>
</body>
<script type="text/javascript">
function caidan(a)
{
    a.style.backgroundColor="red";   //鼠标放上变为红色
}
function out(b)
{
    b.style.backgroundColor="white";    //鼠标移开变回白色
}
function quzhi(c)
{
    var s= c.innerText;
    var y=document.getElementById("menu");    //将选中的值传给表单
    y.value=s;
}
function xiala()
{
    document.getElementById("minzu").style.display="block";  //点击表单元素显示下拉菜单
    stopEventBubble(event);
}
function guan()
{
    document.getElementById("minzu").style.display="none";    //点击菜单外的屏幕部分下拉菜单消失
}

function stopEventBubble(event)    //阻止事件冒泡函数(阻止点击表单元素时下拉菜单消失)
{
    var e=event || window.event;
    if (e && e.stopPropagation)
    {
        e.stopPropagation();
    }
    else
    {
        e.cancelBubble=true;
    }
}

注意:阻止事件冒泡函数(可复制直接使用)

function stopEventBubble(event)    //阻止事件冒泡函数
{
    var e=event || window.event;
    if (e && e.stopPropagation)
    {
        e.stopPropagation();
    }
    else
    {
        e.cancelBubble=true;
    }
}
时间: 2024-08-01 14:11:42

15-08-02 例子:下拉菜单(阻止事件冒泡函数)的相关文章

Bootstrap例子——下拉菜单

<!DOCTYPE html><html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-COMPATIBLE" content="IE=edge"> <meta name="viewport" content="width=device-width,initial-scale=1"

使用select下拉菜单筛选table表格内容

使用select下拉菜单筛选table表格内容:本章节介绍一下如何利用select下拉菜单对表单的内容进行筛选,如果数据量庞大,便于搜索使用的话,筛选效果还是非常有必要的,代码实例如下: <!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://www.softwhy.com/&qu

jquery 实现下拉菜单

Jquery 是一个轻量的框架,个人认为非常好用,今天就写一个非常简单的例子,实现下拉菜单功能: 首先肯定要在页面引用jquery.js  版本不限 : 接下来把=====================html贴出来: Html代码   <div class=”header_menu”> <ul> <li class=”menuli” id=”xtgl_menu”>系统管理</li> <li class=”menuli” id=”ggsq_menu”

【JavaScript】利用onmousemove与onmouseout事件制作兼容IE6纯javascript无插件原生态的下拉菜单dropdown

下拉菜单dropdown在很多前端框架里面都有,直接拿来用就可以的,但是框架内的下拉菜单不好改,也很可能会有兼容性问题. 其实这东西完全可以利用HTML+CSS+Javascript去实现的. 效果如下: 其基本制作思想如下: 首先是HTML与CSS的布局,先布置一个table,这个table里面有两行,一行放下拉按钮,一行放下拉菜单,占据整个宽度的100%,单元格之间的行距为0. 因为共5个元素每列占20%. 这里不用div的float去布局,是因为这里使用table的话,这个下拉菜单组非常好

js阻止事件冒泡和标签默认行为

<a href="/Scripts/newfiber_js_lib/images/1.jpg" ><div onclick="historyImg(this, event)" class="txt_more">更多</div></a> function historyImg(dom,e) { stopBubble(e); stopDefault(e); } //阻止事件冒泡函数 function s

《精通CSS》一个下拉菜单的例子

这个例子是下拉菜单的,我进行了一点点改动,原理的确很简单,但是作者的代码层次感很清楚,值得学习:另外一点,作者在交互过程中的颜色变化,甚至精确到边框线之间的变化,虽然从旁观者的角度看基本上看不出来这个有什么变化,但是这种细致入微的思考和设计是值得学习和思考的,所谓细微之处方见技艺是否成熟,我觉得这是有道理的!不说了,上代码: 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://

JavaScript下拉菜单的例子分享

css+js下拉菜单 完整代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server&q

02 - Unit07:显示笔记下拉菜单、笔记的分享功能、笔记的删除功能

显示笔记下拉菜单 笔记的分享功能 发送Ajax请求 绑定事件:绑定分享按钮单击事件 参数获取:笔记ID 发送请求:/share/add.do 服务器处理 ShareController ShareService ShareDao ShareMapper: inert inot cn_share Ajax回调处理 SUCCESS: 提示分享成功 ERROR:提示分享失败 笔记的删除功能 发送Ajax请求 绑定事件:监听alert页面创建按钮(给按钮增加ID:deleteNote) $("#can&

收藏的js实现全国省份城市级联下拉菜单

在写程序的时候,经常需要用到全国省份城市级联下拉菜单的js代码. 这样的程序,没有必要完全自己去写,太费时间. 下面是我收藏的一个优质代码,非常不错,分享给大家. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http