我在我的html中为标记分配了一个'onchange'属性。 我已经在main.js文件(捆绑到bundle.js中)中成功地做到了。 然后在index.html中引用我的bundle.js。

每当onchange事件尝试使用我定义了filterChanged()的函数时,它都会提供参考错误:“在HTMLSelectElement.onchange上未定义filterChanged”

我本来以为可能是由于函数不是全局变量,所以我分配给了var ,问题仍然存在。

参见下面的代码-

main.js

var filterChanged = function() {

console.log('filter changed');

}

window.onload = function() {

let filter = document.getElementById('filter-list');

filter.setAttribute('onchange', 'filterChanged()');

}

index.html

TestProj

Todos

Number of items to show

10

20

30

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐