日期:2014-05-17 浏览次数:20639 次
$(function() { $(document).mousedown(function(event) { if (event.target.id == 'color') { document.getElementById("x-palette-panel").style.display = 'block'; } else { document.getElementById("x-palette-panel").style.display = 'none'; } }); });
<div id="x-palette-panel" class="panel" style="left: 500px; display: none;"> <span style="color:#000000;background-color:#ffffff;" onclick="highContrast(0)">黑底白字</span> </div>
<html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>无标题页</title> <script type="text/javascript" src="jquery-1.8.1.min.js"></script> </head> <body> <form id="form1" runat="server"> <div id="x-palette-panel" class="panel" style="left: 500px; display: none;"> <span style="color: #000000; background-color: #ffffff;" onclick="highContrast(event,0)"> 黑底白字</span> </div> <img src="http://avatar.profile.csdn.net/C/9/4/2_net_lover.jpg" id="color" /> <div>其他地方的内容</div> </form> <script type="text/javascript"> $(function () { $(document).click(function (event) { if (event.target.id == 'color') { document.getElementById("x-palette-panel").style.display = 'block'; } else { document.getElementById("x-palette-panel").style.display = 'none'; } }); }); function highContrast(e, x) { alert("你点击了 黑底白字 参数=" + x); if (e && e.stopPropagation) {//非IE e.stopPropagation(); } else {//IE window.event.cancelBubble = true; } } </script> </body> </html>
------解决方案--------------------
$(function() { $(document).mousedown(function(event) { if (event.target.id == 'color') { document.getElementById("x-palette-panel").style.display = 'block'; } else if(event.target.id!='x-palette-panel'){ //再做一层判断 document.getElementById("x-palette-panel").style.display = 'none'; } }); });