简体   繁体   English

在按钮附近显示浮动div

[英]Show floated div near a button

I am trying to show a floated div near a button when I click it, and hide the div when I hit a "Close" span within it. 我试图在单击按钮时在按钮附近显示一个浮动div,并在单击其中的“关闭”范围时隐藏div。 This is my code: 这是我的代码:

    <style>
    #noteDiv {display:none; position: absolute;}
    </style> 

    <script>
    function showNote(e) {

      var x = 0, y = 0;
      if (!e) e = window.event;
      if (e.pageX || e.pageY) {
        x = e.pageX;
        y = e.pageY;
      }
      else if (e.clientX || e.clientY) {
        x = e.clientX + document.body.scrollLeft
          + document.documentElement.scrollLeft;
        y = e.clientY + document.body.scrollTop
          + document.documentElement.scrollTop;
      }
    var noteDiv = document.getElementById("noteDiv");
      noteDiv.style.display = "block";
      noteDiv.style.left = (x+20)+"px";
      noteDiv.style.top = (y)+"px";
    }

    function hideNote() {
      document.getElementById("noteDiv").style.display = "none";
    }
    </script>

 <div id='noteDiv'>
 <div style="margin: 5px; float: right; font-size: smaller"><span  onclick="hideNote()">Close</span></div><br clear="all">

<div>Note Content</div>
 </div>

    <?php
    echo "<button type ='button' onClick = 'showNote();'>Note</button>";
    ?>

It does hide the div, but when I click the button "Note", the noteDiv does not show up. 它确实隐藏了div,但是当我单击按钮“ Note”时,noteDiv不会显示。

What is going wrong? 怎么了?

Thank you! 谢谢! Michael Laszlo. 迈克尔·拉斯洛(Michael Laszlo)。 I Think I found the problem though I don't know how to deal with it. 我想我发现了问题,尽管我不知道如何处理。

I believe it is the javascript peice since if I comment out this part: 我相信这是javascript peice,因为如果我注释掉这一部分:

if (e.pageX || e.pageY) {
  x = e.pageX;
  y = e.pageY;
 }
  else if (e.clientX || e.clientY) {
  x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
  y = e.clientY + document.body.scrollTop  + document.documentElement.scrollTop;
  }

The button works well (although the position is at the left corner of the screen since the x and y are 0). 该按钮效果很好(尽管由于x和y为0,所以它的位置在屏幕的左上角)。

Did I miss anything? 我有想念吗?

For Sunil's suggestion,I removed the class part for now, and I changed the onClick to onclick, and the echo is good now, Thank you Sunil! 对于Sunil的建议,我现在删除了类部分,并且将onClick更改为onclick,现在回声很好,谢谢Sunil!

Thank you Michael Laszlo, again! 再次感谢Michael Laszlo! Yes you are right! 是的,你是对的! I need to give the button an ID or the e could not be passed to the function! 我需要给按钮一个ID或e不能传递给函数! Now my code works, too! 现在我的代码也可以使用!

I have a lot of such buttons on my page, and I need to generate a var to verify them and then I think it should work. 我的页面上有很多这样的按钮,我需要生成一个var来验证它们,然后我认为它应该可以工作。 If any problems, it should be another question, haha, about php. 如果有任何问题,那应该是关于PHP的另一个问题,哈哈。

Thank you so much Michael Laszlo and Sunil, your suggestions of setting ids to the button and checking the code from browser are great help! 非常感谢Michael Laszlo和Sunil,关于为按钮设置ID并从浏览器检查代码的建议对您有很大帮助! I learned valuable things from you guys! 我从你们那里学到了有价值的东西!

The problem is that the event e is undefined when showNote() gets called. 问题是调用showNote()时事件e是不确定的。 To ensure that an event is passed to showNote , give the button an ID and attach the click handler when the window loads. 为了确保将事件传递给showNote ,请为按钮提供一个ID,并在窗口加载时附加单击处理程序。

For example, you can define the button like this: 例如,您可以这样定义按钮:

<button id="noteButton" type="button">Note</button>

Then add this to your JavaScript: 然后将其添加到您的JavaScript:

window.onload = function () {
  document.getElementById('noteButton').onclick = showNote;
};

This approach is demonstrated in the following snippet. 下面的代码段演示了这种方法。

 window.onload = function () { document.getElementById('noteButton').onclick = showNote; }; function showNote(e) { var x = 0, y = 0; if (!e) e = window.event; if (e.pageX || e.pageY) { x = e.pageX; y = e.pageY; } else if (e.clientX || e.clientY) { x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft; y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop; } var noteDiv = document.getElementById("noteDiv"); noteDiv.style.display = "block"; noteDiv.style.left = (x+20)+"px"; noteDiv.style.top = (y)+"px"; } function hideNote() { document.getElementById("noteDiv").style.display = "none"; } 
 #noteDiv { display:none; position: absolute; } 
 <div id='noteDiv'> <div style="margin: 5px; float: right; font-size: smaller"> <span onclick="hideNote()">Close</span> </div> <br clear="all"> <div>Note Content</div> </div> <button id="noteButton" type="button">Note</button> 

It should work, but keep in mind that you need to click a specific area on screen and not just anywhere to close the note. 它应该可以工作,但请记住,您需要单击屏幕上的特定区域,而不是单击任意区域以关闭笔记。 You could make this area obvious to user by styling the span with a border. 您可以通过为边框设置样式来使该区域对用户显而易见。

You can see a demo to show that it works here: https://jsfiddle.net/g43j5jdp/1/ 您可以在此处看到一个演示它可以正常工作的演示: https : //jsfiddle.net/g43j5jdp/1/

I added a border to the close area as in code below. 我在封闭区域中添加了边框,如下面的代码所示。

<div style="margin: 5px; float: right; font-size: smaller;
border:1px solid red;"><span onclick="hideNote()">Close</span>

NOTE The quickest solution to your problem would be to use the browser's developer tool. 注意解决您问题的最快方法是使用浏览器的开发人员工具。 Else you might end up spending too much time on this. 否则,您可能最终会花太多时间在此上。

Also please read this stackoverflow post, which might help you to get rid of php code $classStr showing in rendered html: Why is my PHP source code showing? 请阅读这篇stackoverflow帖子,它可能会帮助您摆脱呈现在html中的php代码$ classStr: 为什么显示我的PHP源代码? Your problem seems to be with php configuration. 您的问题似乎与php配置有关。

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

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