簡體   English   中英

獲取觸發事件的元素的id,使用id作為字符串

[英]Get id of an element that triggered the event, utilize id as string

我想使用觸發元素 id,然后將其用作字符串,以執行某些邏輯操作。

基本上

<html>
<head>
<script type="text/javascript">

//OK 
function getValue(control)
{
alert(control.id);
}

function filterByIDType(ctl) {  
var marker = "not set";
var ctlID = ctl.id;
alert(ctlID);  //OK
//alert(ctlID.toString()); //Not working, how to use to match particular 'types'

// Not working:
try
{ 
if(ctlID.Match('Type1'))
 { marker ='Type1'; 
   document.getElementByID(marker+'Lists').value = marker; 
}
if(ctlID.Match('Type2'))
  marker = 'Type2'
if(ctlID.Match('Type3'))
  marker = 'Type3';

}
catch(err)
{
alert('Marker is still:'+marker);
}

}
</script>
</head>
<body>

<h1>My Web Page</h1>
<p id="demo">This is a paragraph.</p>

<a id="idname" onclick="getValue(this)">Click link, get id</a>

<input id="Type1Lists" value="">input type1 here:</input>
<p id="Type1Input" onclick="filterByIDType(this)">Click a paragraph with type1 ip</p>

<p id="Type2Input" onclick="filterByIDType(this)">click a paragraph with type2 ip</p>

<p id="Type3Input" onclick="filterByIDType(this)">click a paragraph with type</p>

</body>
</html> 

我收到錯誤:Object 不支持此屬性或方法

嘗試

if (ctlID.match('Type1'))
//        ^m, not M

制作

var ctlID = ctl.id; 

var ctlID = ctl.id.toString(); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM