document.getElementById("but").onclick = function(e) {
showDropDown(this, e);
};
function showDropDown(element, e) {
element.onclick = function() {};
if (e.stopPropagation) e.stopPropagation(); // W3C model
else e.cancelBubble = true; // IE model
document.getElementById("window").style.display = "inline-block";
document.onclick = function(e) {
var ele = document.elementFromPoint(e.clientX, e.clientY);
if (ele == element) {
hideDropDown();
return;
}
do {
if (ele == document.getElementById("window")) return;
} while (ele = ele.parentNode);
hideDropDown(element);
};
}
function hideDropDown(element) {
document.onclick = function() {};
document.getElementById("window").style.display = "none";
element.onclick = function(e) {
showDropDown(this, e);
};
}
<input id="but" type="button" value="pressMe" />
<div id="window" style="display:none">popup</div>
错误:https://www.dropbox.com/s/uzeiq6043rvueqf/Capture.PNG https://www.dropbox.com/s/w3rct18cumwva7m/bar3.png
答案 0 :(得分:1)
您有错误,因为您的文档未加载
将您的代码放入window.onload
:
window.onload=function(){
//code
}
或者如果您使用的是jquery:
$(document).ready(function(){
//code
});
答案 1 :(得分:0)
document.getElementById("but").onclick = function(e) {
showDropDown(this, e);
};
function showDropDown(element, e) {
element.onclick = function() {};
if (e.stopPropagation)
e.stopPropagation(); // W3C model
else
e.cancelBubble = true; // IE model
document.getElementById("window").style.display = "inline-block";
document.onclick = function(e)
{
var ele = document.elementFromPoint(e.clientX, e.clientY);
if (ele == element) {
hideDropDown();
return;
}
do {
if (ele == document.getElementById("window")) return;
} while ((ele = ele.parentNode) !== null);
hideDropDown(element);
};
}
function hideDropDown(element){
document.onclick = function() {};
document.getElementById("window").style.display = "none";
element.onclick = function(e) {
showDropDown(this, e);
};
}
答案 2 :(得分:0)
您所做的是您复制或编写此代码的地方,其中很可能是一个错误。众所周知JSFiddle有这样的问题。您需要做的是在一个简单的编辑器(如Notepad或TextEdit)中键入该部分代码(错误上方1行,下面1行),然后将其复制并替换当前代码。我知道它的错误,因为它的Unexoected token ILLEGAL
部分意味着放在那里的隐藏字符显然不符合JavaScript,因此它根本不是语法错误。
适合我。