![](/img/trans.png)
[英]Dynamically create a click event based on an array and change format of the clicked element
[英]How to change the result of a click event based on link clicked
在我的代碼中,我從ldap搜索的輸出創建了一個表,輸出(以數組的形式)在第一列中。 我想做的是取決於單擊的鏈接,在第二欄中顯示一些文本。 我已經弄清楚了如何使“ Hello World”出現,但是無論我選擇了什么鏈接,它都會出現。 有沒有一種方法可以根據用戶單擊的鏈接來更改文本? 例如,如果用戶單擊鏈接1,則右列將顯示“你好”,但如果用戶選擇鏈接2,則右列將顯示“再見”。 我的想法是
If (link that's clicked text eq $textToMatch) {
print some text
}
這是我的代碼:
if (($corpId ne "")&&($CorpIdResults eq "")) {
print "This user does not belong to any groups.";
} elsif ($CorpIdResults ne "") {
@splitarray = split(' ',$CorpIdResults);
print "Corp Id: " . $corpId;
print "<BR>";
print "<BR>";
print "This user is a member of the following groups:";
print "<BR>";
print "<TABLE border=22>";
foreach $tmp (@splitarray) {
print "<TR>";
$links = "<a href = 'javascript:testFunction()' id='groups' >$tmp \n</a><BR>";
print "<TD>$links</TD>";
}
print "<TD id='demo'></TD>";
print "</TR>";
print "</TABLE>";
}
希望我已經足夠清楚了。 如果沒有,請告訴我。
要基於某些操作更改javascript函數的行為,您需要向其傳遞一個變量。 您可以通過使鏈接將引用傳遞給自身來在javascript中完成工作:
<a href='#' onclick='testFunction(this); return false;'>Hello</a>
<a href='#' onclick='testFunction(this); return false;'>Goodbye</a>
然后,您可以從函數中使用此引用,例如:
function testFunction(link) {
var text;
if (link.innerHTML == "Hello") {
text = "Hello";
} else if (link.innerHTML == "Goodbye") {
text = "Goodbye";
}
document.getElementById('demo').innerHTML = text;
}
或者,您可以在Perl中進行腿部工作並生成傳遞所需文本的鏈接:
<a href='#' onclick='testFunction("Hello"); return false;'>Hello</a>
<a href='#' onclick='testFunction("Goodbye"); return false;'>Goodbye</a>
然后直接在javascript中使用文本:
function testFunction(text) {
document.getElementById('demo').innerHTML = text;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.