[英]html/javascript: how to onclick on non-existent element in html
當我在javascript中創建但在html中不存在的元素時,如何在單擊“選項”(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)" > ...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.