[英]Javascript - I can't make the “this” keyword on my html
目前,這是我的簡化代碼,無法正常工作。 我想做的是讓兩個輸入(id的nono1和nono2)調用相同的函數(nonoto)。 我不能使用document.getElementById(),因為它們的ID不同。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript" type="text/javascript">
function nonoto()
{
alert(document.getElementById(this.id).value);
}
</script>
</head>
每當你使用nonoto
功能,通過電流輸入元素對象作為this
:
<input id="one" onchange="nonoto(this)" >
功能將變為:
function nonoto(obj)
{
alert(obj.value);
}
因此,該函數不必知道任何id即可獲取元素值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.