JavaScript'OnClick'和HTML <a href=""> Not Working When Used Together

时间:2018-10-05 17:29:12

标签: javascript jquery html

I am trying to perform certain functions when the next button is clicked but when I use onclick with <a href> it doesn't work. I've tried to use JavaScript and jQuery methods but both failed.

Here is the HTML next button code:

<a href="#" id="nextButt" onclick="performNextAction()" class="next round">&#8250;</a>

Here is the jQuery Syntax:

$("nextButt").on('click', function() {
  alert(""You clicked Next Button in Jquery");
})

Here is the JavaScript Syntax

function performNextAction(){
  alert("You clicked Next Button in Javascript")
}

2 个答案:

答案 0 :(得分:1)

这对我有用:

 <a id="nextButt" onclick="performNextAction()" class="next round">&#8250;</a>

<script>
function performNextAction(){
      alert("You clicked Next Button in Javascript");
      window.location.href = "#";
}
</script>

答案 1 :(得分:-1)

我发现了错误。我用JQuery解决了。

这是工作代码:

$("#nextButt").on('click', function() {
alert("You clicked Next Button in Jquery");

})

错误是什么?

您会注意到,在我编写的JQuery代码的第一行中,我省略了“#”符号。 请参阅此处:$("nextButt").on('click', function()

,但是现在带有“#”符号,它可以很好地工作。

我殴打自己,只是四处走走,然后回来看看我脸上的虫子。Ph!哈哈!