I want to call a specific <a>
Element where the href is a JavaScript function. The call shall be from JavaScript code.
For example if we have a tag <div id="ask-question">
. We will call that from javascript by $("#ask-question").onClick()
. For now, my problem is <a>
tag without id. How I can do it?
Full example of my <a>
tag:
<a class="update-receive" href="javascript:void(0)">Update & Reward</a>
答案 0 :(得分:5)
使用
$("a.update-receive").click(function () {
console.log("Update & Reward");
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class="update-receive" href="javascript:void(0)">Update & Reward</a>
答案 1 :(得分:3)
选项1 :(纯JavaScript)
您可以通过onclick
调用函数,如下所示:
当前:
<a class="update-receive" href="javascript:void(0)">Update & Reward</a>
更改为:
<a class="update-receive" href="#" onclick="myFunction();">Update & Reward</a>
演示:
function myFunction() {
console.log("Update & Reward clicked");
}
<a class="update-receive" href="#" onclick="myFunction();">Update & Reward</a>
选项2:(jQuery)
使用jquery
按类名调用函数
$(".update-receive").click(function () {
console.log("Update & Reward clicked");
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class="update-receive" href="#">Update & Reward</a>