简体   繁体   English

html 锚标记的 onclick 不起作用

[英]html anchor tag's onclick was not working

I was generating a table dynamically.我正在动态生成一个表。 Inside the td data , there is a hyperlink.inturn it should call a function.在 td data 里面,有一个超链接。inturn 它应该调用一个函数。 why the below code snippet was not working.为什么下面的代码片段不起作用。 Nothing happening when i click on the link.当我点击链接时什么也没有发生。

  <a href='#' onclick='function(){alert('hiii');}'>

It's because of conflicting ' and a funciton definition instead of call (thanks @user1389596):这是因为冲突的'和函数定义而不是调用(感谢@user1389596):

<a href='#' onclick="alert('hiii')">

that should work.那应该工作。 The extra ' s inside of the onclick made the browser view it as the end of the onclick attribute, which isn't what you want.额外的' S的内部的onclick所做的浏览器视图它作为onclick属性,这是不是你想要的结束。 This way, using two different types of quotes, it works fine.这样,使用两种不同类型的引号,效果很好。

Or, even better, don't use inline handlers:或者,更好的是,不要使用内联处理程序:

<a href='#' id="thethingy">
<!--in a galaxy far, far away:-->
<script type="text/javascript">
    document.addEventListener('DOMContentLoaded',function(){
        document.getElementById('thethingy').addEventListener('click',function(){
            alert('hiii');
        },false);
    },false);
</script>

Event handlers added via innerHTML will not trigger in some cases for security reasons.出于安全原因,通过innerHTML添加的事件处理程序在某些情况下不会触发。 It's also incorrect to include function () since all this onclick would do is define an anonymous function and do nothing else.包含function ()也是不正确的,因为onclick所做的只是定义一个匿名函数onclick做其他任何事情。 You also have conflicting quotes.您也有相互矛盾的报价。

If you are using jQuery, event delegation would make this simpler.如果您使用 jQuery,事件委托会使这更简单。 Exclude onclick entirely and use something like:完全排除onclick并使用以下内容:

$(document).on("click", "a", function () { alert("hiii"); });

document and a selectors should be as specific as possible. documenta选择器应该尽可能具体。

You can also bind events to elements added to the DOM as needed.您还可以根据需要将事件绑定到添加到 DOM 的元素。

1st Issue第一期

onclick='function(){alert('hiii');}'

This seriously is wrongly coded as per the use of quotes .根据引号使用,这严重被错误地编码。 The proper usage should be any of the following正确的用法应该是以下任何一种

Proper use of quotes正确使用引号

onclick='function(){alert("hiii");}'

or

onclick="function(){alert('hiii');}"

This much only about the syntax.这仅与语法有关。 But there is still something to be done to actually call the function at runtime.但是仍然需要做一些事情才能在运行时实际调用该函数。 Without proper invocation the function will not be executed.如果没有适当的调用,该函数将不会被执行。 That is the 2nd issue.那是第二个问题。

2nd Issue第二期

Now there are 2 syntax that you may follow to actually execute the function at runtime.现在,您可以遵循 2 种语法在运行时实际执行该函数。

1st syntax:第一种语法:

(function(){...})();

So the code should be-所以代码应该是-

onclick="(function(){alert('hiii');})();"

Fiddle Demo1小提琴演示1

2nd syntax第二种语法

new function(){...};

So the code should be-所以代码应该是-

onclick="new function(){alert('hiii');};"

Fiddle demo2小提琴演示2

您正在定义一个函数……而不是调用它。

试试这个:

 <a href="javascript:alert('hi');">

You should not use onclick="function(){alert('hiii');}" .你不应该使用onclick="function(){alert('hiii');}" You may try this code:你可以试试这个代码:

<a href="#" onclick="alert('hiii');">
<a onclick='function(){alert('hiii');}' href='#' >

试试这个它会毫无问题地工作,不需要事件监听器

Although this a quotes problem.虽然这是一个引号问题。 Following will solve your problem.以下将解决您的问题。

<a href='#' onclick="alert('hiii')">

In case you are using Vue.js like me onclick will not work.如果您像我一样使用 Vue.js,则 onclick 将不起作用。 You can use this.你可以用这个。

<a href="javascript:void(0);" @click="alert('hiii')">

Maybe this can be of help to someone, At first I thought it was due to the fact that href was present, and inserted before the onclick event.也许这可以对某人有所帮助,起初我认为这是由于存在 href 并在 onclick 事件之前插入的事实。

However here is a sample of an a tag that calls a function and passing a parameter.但是,这里是一个调用函数并传递参数的 a 标记示例。

Both Samples has the onclick event and href properties in different orders.两个示例都具有不同顺序的 onclick 事件和 href 属性。

 function helloWorld(message){ alert('Hello world! : ' + message); };
 <a href='#' onclick="helloWorld('href before onclick')">Click Me (href then onclick)</a> <br> <br> <a onclick="helloWorld('onclick before href')" href="#">Click Me (onclick then href)</a>

}; };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM