[英]JavaScript: onclick function to change another elements onclick
預期功能
我正在尋找像這樣操作的東西:
我的問題:
下面確實“啟用”了鏈接,但它也表現為鏈接被同時點擊。
有沒有辦法修復這個功能?
HTML代碼:
<a href="@Url.Action("Index/" + @card.ID)" onclick="return false;" class="link">
<img src="~/Resources/@card.Image" id="@card.ID" onclick="handClick(@((int)card.Value), @card.ID)" class="card" />
</a>
JavaScript 代碼
function handClick(cardValue, cardID) {
*... irrelevant code ...*
var playedCard = document.getElementById(cardID);
*... irrelevant code ...*
playedCard.parentElement.onclick = function () { return true };
}
由於我不知道不相關的代碼是什么,所以我只會停留在第一次點擊啟用問題上。
我的方法包括href
屬性。 沒有它,鏈接不起作用。 因此,我所做的是將屬性名稱更改為其他名稱,並在單擊時將其替換為實際屬性,從而“激活”鏈接。
請注意,第一次單擊后,光標會變為指針。
document.querySelectorAll('a[hrefx]').forEach(function(el) { el.addEventListener('click', function() { // This condition enable re-using the listener for other purposes. if (this.getAttribute('hrefx')) { this.setAttribute('href', this.getAttribute('hrefx')); this.removeAttribute('hrefx'); } }); });
<a hrefx="#"> <img src="https://picsum.photos/200"> </a> <a hrefx="#"> <img src="https://picsum.photos/200"> </a>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.