简体   繁体   English

关于javascript onclick的问题

[英]Issue regarding javascript onclick

I am creating a link tag (anchor tag) dynamically using javascript . 我正在使用javascript动态创建链接标记(anchor tag) There is a javascript function which will be fired by an event and it will create a javascript link. 有一个javascript函数将由事件触发,它将创建一个javascript链接。

I have already mentioned the required attributes for the newly created anchor tag using javascript. 我已经提到了使用javascript新创建的锚标记所需的属性。 Now I have also mentioned an onclick event on that anchor tag. 现在,我还提到了该锚标记上的onclick事件。

The problem is 问题是

that the onclick event is fired during the anchor tag creation. 在锚标记创建期间触发了onclick事件。 And it is firing for that one time. 这是一次射击。 Next time when I am clicking on the link, I am unable to get my desired result. 下次当我单击链接时,无法获得所需的结果。

javascript code: JavaScript代码:

function waybill()
{
    var mail_link = document.createElement("a");
    mail_link.href = "javascript:void(0)";
    mail_link.className = 'animated bounceInDown';
    mail_link.innerHTML = "Mail Waybill";
    mail_link.onclick = abc_test();
    var holder_div = document.getElementById("holder");
    holder_div.appendChild(mail_link);
}
function abc_test()
{
    alert("mail link clicked");
}

I am getting the alert only once and without even clicking. 我只收到一次警报,甚至没有单击。 Please help me. 请帮我。

mail_link.onclick = abc_test() will invoke abc_test and assign its return value to mail_link.onclick . mail_link.onclick = abc_test()将调用abc_test并将其返回值分配给mail_link.onclick

If you just want to reference the function, and not call it, leave out the () : 如果您只想引用该函数而不要调用它,则省略()

mail_link.onclick = abc_test;

Adding event listeners is one of those things that a lot of old browsers are doing in their own way, and it's a bit messy to add support for all of them. 添加事件侦听器是许多旧版浏览器以自己的方式执行的操作之一,并且添加对所有这些浏览器的支持也有些混乱。 Sicnce the question is tagged jQuery, you could do all of this in jQuery and have browser support handled for you: 由于该问题被标记为jQuery,因此您可以在jQuery中完成所有这些操作,并为您处理浏览器支持:

$('<a/>', {
   href: 'javascript:void(0);',
   'class': 'animated bounceInDown',
   text: 'Mail Waybill',
}).appendTo('#holder').click(abc_test);

The problem is that calling abc_test() will execute the function while using only abc_test will pass a reference to the function. 问题在于,调用abc_test()将执行该函数,而仅使用abc_test将传递对该函数的引用。 in this case you need to change the line: 在这种情况下,您需要更改以下行:

mail_link.onclick = abc_test();

with the line: 与行:

mail_link.onclick = abc_test;

It's because that you've invoked the function instead of referencing it. 这是因为您调用了函数而不是引用它。


mail_link.onclick=abc_test(); mail_link.onclick = abc_test(); - This will invoke the function while initializing mail_link.onclick=abc_test; -这将在初始化mail_link.onclick = abc_test时调用该函数 - This will add a reference of the function to onClick, so that it will invoke the function while you click the anchor link. -这会将函数的引用添加到onClick,以便在单击锚链接时调用该函数。

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

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