简体   繁体   English

为什么我不能单击此元素?

[英]Why can't I click this element?

If you select this text, a div will appear with colors. 如果选择此文本,则div将显示颜色。 I'm trying to track if one of these colors or divs ( .boxes ) are clicked. 我正在尝试跟踪是否单击了这些颜色或div( .boxes )中的一种。 But I can't. 但是我不能。 This is basic JS and it's not working. 这是基本的JS,无法正常工作。 Why? 为什么?

 $(document).ready(function() { $(".boxes").click(function() { alert("Hello"); }); }); $("#actual_verse").mouseup(function() { var text = ""; if (window.getSelection) { text = window.getSelection().toString(); } else if (document.selection && document.selection.type != "Control") { text = document.selection.createRange().text; } if (/\\S/.test(text)) { new_text = "<div id='color_div'>"+text+"</div>"; // Tool Tip var ele = document.getElementById('tooltip'); var sel = window.getSelection(); var rel1= document.createRange(); rel1.selectNode(document.getElementById('cal1')); var rel2= document.createRange(); rel2.selectNode(document.getElementById('cal2')); window.addEventListener('mouseup', function () { if (!sel.isCollapsed) { debugger; var r = sel.getRangeAt(0).getBoundingClientRect(); var rb1 = rel1.getBoundingClientRect(); var rb2 = rel2.getBoundingClientRect(); ele.style.top = (r.bottom - rb2.top)*100/(rb1.top-rb2.top) + 'px'; //this will place ele below the selection ele.style.left = (r.left - rb2.left)*100/(rb1.left-rb2.left) + 'px'; //this will align the right edges together //code to set content ele.style.display = 'block'; } }); window.addEventListener('mousedown', function () { ele.style.display = 'none'; }); // End of Tool Tip } }); 
 /* Tool Kit */ #tooltip { position:absolute; display:none; border:grey solid 1px; background: #373737; padding: 5px; border-radius: 3px; } #cal1{ position:absolute; height:0px; width:0px; top:100px; left:100px; overflow:none; z-index:-100; } #cal2{ position:absolute; height:0px; width:0px; top:0; left:0; overflow:none; z-index:-100; } .boxes { width: 15px; height: 15px; cursor: pointer; display: inline-block; margin-right: 2px; position: relative; top: 3px; } #blue_box { background: #AAF6FF; } #green_box { background: #D6FFAA; } #orange_box { background: #FFBF98; } #purple_box { background: #D7D5FC; } #red_box { background: #FF9B9F; } #yellow_box { background: #FFF8AA; } /* End of Tool Kit */ 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id='actual_verse'> Hello g mjfrmjrern erjnejnef jfejnfeijmfijfeifeef ejngjrrgrg Hello g mjfrmjrern erjnejnef jfejnfeijmfijfeifeef ejngjrrgrg Hello g mjfrmjrern erjnejnef jfejnfeijmfijfeifeef ejngjrrgrg Hello g mjfrmjrern erjnejnef jfejnfeijmfijfeifeef ejngjrrgrg Hello g mjfrmjrern erjnejnef jfejnfeijmfijfeifeef ejngjrrgrg Hello g mjfrmjrern erjnejnef jfejnfeijmfijfeifeef ejngjrrgrg </div> <div id='cal1'>&nbsp;</div> <div id='cal2'>&nbsp;</div> <div id='tooltip'> <div id='blue_box' class='boxes' title='Blue' onclick='box()'></div> <div id='green_box' class='boxes' title='Green'></div> <div id='orange_box' class='boxes' title='Orange'></div> <div id='purple_box' class='boxes' title='Purple'></div> <div id='red_box' class='boxes' title='Red'></div> </div> <br> <br> 

The problem is this code: 问题是此代码:

window.addEventListener('mousedown', function() {
  ele.style.display = 'none';
});

The tooltip is being hidden on mousedown, which fires before the click on the "boxes" class. 该工具提示在mousedown上被隐藏,它会在单击“ boxes”类之前触发。 So the click never completes. 因此,点击永远不会完成。 Move this code into the click function - demo 将此代码移入click功能- 演示

$(".boxes").click(function() {
  alert("Hello");
  document.getElementById('tooltip').style.display = 'none';
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM