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
发布于 2012-08-26 14:07:41
你所做的就是在你复制或写这段代码的地方,很可能里面有一个bug。众所周知,JSFiddle就有这样的问题。您需要做的就是在记事本或TextEdit等简单编辑器中键入这段代码(错误上方1行,错误下方1行),然后将其复制并替换当前代码。我知道是这个错误,因为它的Unexoected token ILLEGAL部分意味着放在那里的隐藏字符显然不符合JavaScript,因此它根本不是一个语法错误。
这对我很有效。
发布于 2012-08-26 14:01:04
您有错误,因为您的文档未加载。
将您的代码放在window.onload中
window.onload=function(){
//code
}或者,如果您使用的是jquery:
$(document).ready(function(){
//code
});发布于 2012-08-26 14:00:30
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);
};
}https://stackoverflow.com/questions/12127932
复制相似问题