Javascript onclick函数立即被调用(而不是点击)? [英] Javascript onclick function is called immediately (not when clicked)?

查看:637
本文介绍了Javascript onclick函数立即被调用(而不是点击)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个链接,它看起来和感觉像一个< a> 标记项目,但运行一个函数而不是使用href。



当我尝试将onclick函数应用于链接时,它立即调用该函数,而不管链接从未被点击的事实。任何尝试点击链接后失败。



我做错了什么?


$ b HTML

 < div id =parent> 
< a href =#id =sendNode>发送< / a>
< / div>

Javascript

  startFunction(); 

function secondFunction(){
window.alert(Already called !?);


function startFunction(){
var sentNode = document.createElement('a');
sentNode.setAttribute('href',#);
sentNode.setAttribute('onclick',secondFunction());
//sentNode.onclick = secondFunction();
sentNode.innerHTML =已发送邮件;

//将新元素添加到父元素
var parentNode = document.getElementById('parent');
var childNode = document.getElementById('sendNode');
parentNode.insertBefore(sentNode,childNode);
}

JsFiddle



正如你所看到的,我尝试了两种不同的方式来添加这个onclick函数,它们都具有相同的效果。

解决方案

您需要 .onclick = secondFunction


$ b NOT .onclick = secondFunction()
< hr>

后者调用(执行) secondFunction ,而前者传递对 secondFunction onclick 事件






  function start(){var a = document.createElement(a); a.setAttribute(href,#); a.onclick = secondFunction; a.appendChild(document.createTextNode(click me)); document.body.appendChild(a);} function secondFunction(){window.alert(hello!);} start();  

/ docs / Web / API / EventTarget / addEventListenerrel =nofollow> elem#addEventListener

  a。 addEventListener(click,secondFunction); 

// OR

a.addEventListener(click,function(event){
secondFunction();
event.preventDefault();
});


I am trying to create a link, which looks and feels like an <a> tag item, but runs a function instead of using the href.

When I try to apply the onclick function to the link it immediately calls the function regardless of the fact that the link was never clicked. Any attempt to click the link thereafter fails.

What am I doing wrong?

HTML

<div id="parent">
    <a href="#" id="sendNode">Send</a>
</div>

Javascript

startFunction();

function secondFunction(){
    window.alert("Already called!?");
}

function startFunction() {
    var sentNode = document.createElement('a');
        sentNode.setAttribute('href', "#");
        sentNode.setAttribute('onclick', secondFunction());
      //sentNode.onclick = secondFunction();
        sentNode.innerHTML = "Sent Items";

    //Add new element to parent
    var parentNode = document.getElementById('parent');
    var childNode = document.getElementById('sendNode');
    parentNode.insertBefore(sentNode, childNode);
}

JsFiddle

As you can see I tried two different ways of adding this onclick function, both of which have the same effect.

解决方案

You want .onclick = secondFunction

NOT .onclick = secondFunction()


The latter calls (executes) secondFunction whereas the former passes a reference to the secondFunction to be called upon the onclick event


function start() {
  var a = document.createElement("a");
  a.setAttribute("href", "#");
  a.onclick = secondFunction;
  a.appendChild(document.createTextNode("click me"));
  document.body.appendChild(a);
}

function secondFunction() {
  window.alert("hello!");
}

start();


You could also use elem#addEventListener

a.addEventListener("click", secondFunction);

// OR

a.addEventListener("click", function(event) {
  secondFunction();
  event.preventDefault();
});

这篇关于Javascript onclick函数立即被调用(而不是点击)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆