showModal() in Javascript not working when inside an if statement in a function

huangapple go评论80阅读模式
英文:

showModal() in Javascript not working when inside an if statement in a function

问题

我试图在if语句为真时显示一个模态框,在我的示例中,我在用户按Enter键后这样做,在我的实际代码中,还有一些嵌套在其中的其他if语句。

我编写了[这个CodePen链接](https://codepen.io/nbonato/pen/bGQgzap)来说明这个问题,并且我也将代码放在这里以便清晰地查看。

这是我的HTML,非常基本,创建了对话框并未提供“open”属性,因此默认情况下它是隐藏的:

<dialog id="greeting">
  <p>问候,所有人!<p>
  <form method="dialog">
    <button>×</button>
  </form>
</dialog>

然后我们有JavaScript,我在其中获取模态框并将其存储在全局变量中,创建一个监听按键的函数和另一个用于解析它们的函数。在解析函数内部,我添加了(不成功地)仅在按下的键是“ENTER”时显示模态框的代码。

let greeting = document.getElementById("greeting");
window.addEventListener("keydown", function (e) {
    parseKey(e.key);
});

function parseKey(pressedKey) {
    pressedKey = pressedKey.toUpperCase()
    if (pressedKey == "ENTER") {
      console.log(greeting);
      greeting.showModal();
      console.log(greeting.getAttribute("open"));
      
      //greeting.setAttribute("open", "open");
      
      //alternative = document.getElementById("greeting");
      //console.log(alternative);
      //alternative.showModal();
    }
}

如你所见,我还:

1. 使用`console.log()`作为一种调试工具,以确保if语句起作用,并确保模态框实际上在函数内部是可用的。这似乎有效,因此当我按Enter键时,控制台会显示模态框的HTML代码。
2. 记录模态框的“open”属性的值,得到一个空字符串作为响应。

在注释掉的部分中,我还尝试过:
1. 手动设置属性
2. 直接在函数内部获取模态框,再次得到相同的结果,函数将模态框的HTML记录到控制台中,但模态框仍然保持隐藏。

当我尝试连续使用两个`showModal()`时,我得到了`InvalidStateError: HTMLDialogElement.showModal: Dialog element already has an 'open' attribute`,这让我觉得我显然漏掉了什么,因为模态框仍然是不可见的。

感谢所有能够提供帮助的人。
英文:

I am trying to show a modal after an if statement is true, in my example I am doing this after the user presses Enter, in my real code there are a few other if statements nested into that one.

I wrote this codepen to illustrate the issue, and I am also putting the code here for clarity.

This is my HTML, pretty basic, creating the dialog and not providing the "open" attribute, so it's hidden by default:

Press Enter
<dialog id="greeting">
  <p>Greetings, one and all!<p>
  <form method="dialog">
    <button>×</button>
  </form>
</dialog>

Then we have the Javascript where I grab the modal and store it in a global variable, create a function listening for pressed keys and another one that parses them. Inside the parsing function, I added the code to (unsuccessfully) show the model only if the pressed key is "ENTER".

let greeting = document.getElementById("greeting");
window.addEventListener("keydown", function (e) {
    parseKey(e.key);
});

function parseKey(pressedKey) {
    pressedKey = pressedKey.toUpperCase()
    if (pressedKey== "ENTER") {
      console.log(greeting);
      greeting.showModal();
      console.log(greeting.getAttribute("open"));
      
      
      //greeting.setAttribute("open", "open");
      
      //alternative = document.getElementById("greeting");
      //console.log(alternative);
      //alternative.showModal();
    }
}

As you can see, I also:

  1. Used console.log() as a sort of debugging tool to make sure the if statement worked and that the modal was actually available inside the function. That seems to work, so when I press Enter, the console shows the HTML code of the modal
  2. Logging the value of the "open" attribute for the modal, getting an empty string as a response

In the commented out part I also tried:

  1. To set the attribute manually
  2. To grab the modal directly inside the function, once again getting the same result of the function logging to the console the HTML of the modal, but the modal remaining hidden

When I tried putting two showModal() one after the other, I got the InvalidStateError: HTMLDialogElement.showModal: Dialog element already has an 'open' attribute, which makes me think I am clearly missing something, since the modal is still invisible.

Thanks to everybody that can help.

答案1

得分: 2

"you need to prevent the native event to be executed.
Add e.preventDefault(); and everything should work fine."
你需要阻止原生事件的执行。
添加 e.preventDefault();,一切应该正常工作。

英文:

you need to prevent the native event to be executed.
Add e.preventDefault(); and everything should work fine.

huangapple
  • 本文由 发表于 2023年6月27日 18:25:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76563920.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定