我有這個代碼
<div style="float:left">
<div class="d-inline ">
<i id="reply@{@item.Id}" class="bi bi-reply icon-cursor test-comment" onclick="CommentReply(@item.Id)"></i>
</div>
</div>
和js
function CommentReply(id)
{
//to do something
}
但我想改成
$(".test-comment").click(function () {
//to do something
// I need Id in here
})
如何將 Id 引數傳遞給測驗評論點擊事件?
uj5u.com熱心網友回復:
嘗試使用資料屬性:測驗代碼here
HTML
<div style="float:left">
<div class="d-inline ">
<i data-id="10" data-xx="other x" class="bi bi-reply icon-cursor test-comment" >some text</i>
</div>
</div>
洗掉 onclick= ... 并使用 data-* 作為您的資訊。
JS
$(".test-comment").click(function () {
alert($(this).data("id"));
alert($(this).data("xx"));
})
片段 <3
$(".test-comment").click(function () {
alert($(this).data("id"));
alert($(this).data("xx"));
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div style="float:left">
<div class="d-inline ">
<i data-id="10" data-xx="other x" class="bi bi-reply icon-cursor test-comment" >CLICK ME!</i>
</div>
</div>
uj5u.com熱心網友回復:
使用this純 JS,helloo僅用于測驗
function CommentReply(el){
// `el` here is `this` passed from `onclick` is the reference to the element
console.log(el.id);
}
<div style="float:left">
<div class="d-inline ">
<i id="reply@{@item.Id}" class="bi bi-reply icon-cursor test-comment" onclick="CommentReply(this)">helloo</i>
</div>
</div>
點擊hello并查看控制臺的 id
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/461222.html
標籤:javascript jQuery
上一篇:js決議sql并提取表名別名
