javascript - html/javascript : how to onclick on non-existent element in html

标签 javascript html css

单击“选项”时如何调用函数(HTML 选择 > 选项) 当该元素是在 javascript 中创建而在 html 中不存在时? 这里我没有使用 jquery 或 ajax,只是 javascript

在我的 html 中我只有:

<fieldset>
    <legend>List...</legend>

    <select id="select1" size="9" style="width:150px">
        <option>Select a person</option>
    </select>
</fieldset>
  • 更多选项(人员)在另一个功能中创建

最佳答案

使用onchange事件

不使用 jquery

document.body.onchange = function(e) {
    if (e.target.tagName=='SELECT'){
        SelectedOption(e.target);
    }
}
function SelectedOption(option){
    alert(option.value);
    alert(option.id);
}

选择控件的另一个例子

<select onchange="javascript:SelectedOption(event.target)" > ...

关于javascript - html/javascript : how to onclick on non-existent element in html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38938675/

相关文章:

html - 登录表单显示在页面中央

html - CSS 网格 - 2x2 网格总是尽可能占据整个宽度

javascript - 转义键未触发 keyup 事件

css - 在 Safari 中使用透明颜色会发生什么?

JavaScript - 交换颜色

javascript - Javascript 片段的正确且优雅的格式/语法

javascript - 如何在不使用 Select ID 的情况下创建基于下拉选择的 jQuery URL 重定向?

javascript - "Modernizr is not defined"

javascript - 如何修复提交按钮激活错误?

php - 调用函数时在脚本函数中运行 php 代码