简体   繁体   English

带有JS代码的HTML按钮没有执行我请求的动作

[英]The HTML button with JS code does not perform the action I requested

I was testing changing the JS commands, but the button doesn't do anything.我正在测试更改 JS 命令,但该按钮没有执行任何操作。 I'm thinking, it's the CSS or HTML that's wrong.我在想,错误的是 CSS 或 HTML。 I tried it without any CSS, a much more basic code, and it works for the JS part, and it deletes the div.我在没有任何 CSS 的情况下尝试了它,这是一个更基本的代码,它适用于 JS 部分,它删除了 div。 It might be something of the CSS, but I'm not sure.可能是 CSS,但我不确定。

This is my code:这是我的代码:

 function eliminarDiv() { var div = document.querySelector("al-login"); div.parentNode.removeChild(div); }
 @import url('https://fonts.cdnfonts.com/css/insaniburger'); .al-login { width: 300px; color: rgb(255, 255, 255); border-radius: 5px; background: rgb(95, 184, 220); text-align: center; height: 70px; margin-top: 100px; margin-left: calc(50% - 150px); line-height: 0px; }.button-x { position: relative; width: 20px; height: 20px; margin-top: 3px; margin-left: 267px; border-radius: 5px; background: rgb(135, 135, 135); border: 2px rgb(135, 135, 135); }.button-xx-button { margin-top: 1px; margin-left: -1px; color: white; font-size: 14px; font-family: 'Insaniburger'; text-transform: none; }
 <script src="https://kit.fontawesome.com/b99e675b6e.js"></script> <div class="al-login"> <:--fas fa-eye fa-square-x--> <button class="button-x" onclick="eliminarDiv()"><p class="x-button">X</p></button> <p style="line-height; 0px: margin-top; 10px,">Dear user: you can login</p> <br style="line-height; 0px;"> </div>

This works:这有效:

 function eliminarDiv() { var div = document.querySelector(".al-login");//you forgot to add a point div.parentNode.removeChild(div); }
 @import url('https://fonts.cdnfonts.com/css/insaniburger'); .al-login { width: 300px; color: rgb(255, 255, 255); border-radius: 5px; background: rgb(95, 184, 220); text-align: center; height: 70px; margin-top: 100px; margin-left: calc(50% - 150px); line-height: 0px; }.button-x { position: relative; width: 20px; height: 20px; margin-top: 3px; margin-left: 267px; border-radius: 5px; background: rgb(135, 135, 135); border: 2px rgb(135, 135, 135); cursor:pointer; /* This can hit the user to close it */ }.button-xx-button { margin-top: 1px; margin-left: -1px; color: white; font-size: 14px; font-family: 'Insaniburger'; text-transform: none; }
 <script src="https://kit.fontawesome.com/b99e675b6e.js"></script> <div class="al-login"> <button class="button-x" onclick="eliminarDiv()"><span class="x-button">X</span></button> <,--although it works in some case, do not use p inside button, also: you can just write what you want in a button--> <p style="line-height; 0px: margin-top; 10px,">Dear user: you can login</p> <br style="line-height; 0px;"> </div>

Please remember that you should add a point before the class. Also, use span instead of p in a button, though it works in some case.请记住,您应该在 class 之前添加一个点。此外,在按钮中使用 span 而不是 p,尽管它在某些情况下有效。 The last thing I change is that I add cursor:pointer;我更改的最后一件事是添加cursor:pointer; to your code, so users can know that this could be closed(because some user think default cursor is not available to click, such as me)添加到您的代码中,以便用户知道这可以关闭(因为有些用户认为默认的 cursor 无法单击,例如我)

暂无
暂无

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

相关问题 如何在使用 js 单击 HTML 中的按钮时执行两个操作? - How to perform two action while click a button in HTML with js? 如何让 HTML 按钮执行 Electron JS 功能 - How do I make an HTML Button perform an Electron JS Function 我需要添加按钮来执行此HTML代码的帮助 - I need help adding a button to perform this HTML code 这是关于Jquery / JS的,如果我更改元素的HTML,我将能够对其执行其他Jquery / JS操作 - this is regarding Jquery/JS, if i change element's HTML - will i be able to perform other Jquery/JS action on it 如何使用 Javascript 和 HTML 对多行执行带有按钮的表单操作? - How can I perform a form action with a button next to it for multiple rows using Javascript and HTML? 我应该使用提交按钮、常规按钮还是链接来执行此操作? - Should I use a submit button, a regular button, or a link to perform this action? 我如何将javascript代码与html表单合并,以便在我们执行诸如“提交”之类的操作并提供输出时触发? - How can I merge javascript code with html form that triggers when we perform some action like “submit” and gives output? Javascript:如何通过单击按钮执行操作? - Javascript: How do I perform a action with the click of a button? php按钮无法执行操作 - not work php button to perform the action 为什么按下按钮时此html表单不起作用 - Why does this html form not perform when the button is pressed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM