【问题标题】:Uncaught TypeError: Cannot read property 'undefined' of undefined at HTMLFormElement.onmouseover未捕获的类型错误:无法在 HTMLFormElement.onmouseover 处读取未定义的属性“未定义”
【发布时间】:2019-07-31 09:33:35
【问题描述】:

我正在使用这个 sn-p:

onmouseover="this.title=this.options[this.selectedIndex].title"

以便我可以在下拉菜单文本中添加 title 标签。

<form name="searchform" onmouseover="this.title=this.options[this.selectedIndex].title" onsubmit="return doSearch();" accept-charset="', $context['character_set'], '">
<input class="search_input" type="text" name="search" value="" />
<input class="search_button" type="submit" name="submit" value="" />
<select class="search_select" name="sengines" onchange="return saveOption();">
<option value="?action=search2;search=" title="Full forum search">Forum</option>

我使用浏览器工具检查时遇到的错误是

Uncaught TypeError: Cannot read property 'undefined' of undefined at HTMLFormElement.onmouseover 

【问题讨论】:

  • 先确定某事被选中!最好使用addEventListener 而不是onmouseover 并使用事件处理程序,您可以在其中测试是否确实选择了某事,而不是在html onmouseover 属性中硬编码处理程序
  • 谢谢,不知道如何在代码上实现...
  • 首先尝试在表单元素上添加onmouseover,而不是在select 元素上。然后阅读addEventListener并使用它
  • 有趣的是,标题标签在没有 JavaScript sn-p 的情况下仍然显示。所以解决方案更简单......

标签: javascript forms smf-forum


【解决方案1】:

这种情况下需要绑定事件监听的功能

<form name="searchform" onmouseover="function(this)" onsubmit="return doSearch();" accept-charset="', $context['character_set'], '">

这意味着当前标签/元素将通过事件的参数传递

 onmouseover="function(this)"

你可以在你想要的元素中添加事件,或者像你一样访问子元素

this.options[this.selectedIndex].title

【讨论】:

    猜你喜欢
    • 2015-01-06
    • 2017-07-26
    • 2021-12-22
    • 1970-01-01
    • 2019-02-26
    • 2018-12-03
    • 2017-06-05
    相关资源
    最近更新 更多