简体   繁体   English

使用脚本设置onclick事件

[英]Set onclick event using script

I want to make my script set the onclick properity of a <div> .我想让我的script设置<div>onclick属性。

I use this Html code:我使用这个 Html 代码:

<div id="forgotpass">Forgot Password?</div>

I want when a user clicks the <div> a forgotpass() function to run, but I do not want to use this:我希望当用户单击<div>时运行一个forgotpass()函数,但我不想使用它:

<div id="forgotpass" onclick="forgotpass();">Forgot Password?</div>

Alternatively, if you're not using jQuery:或者,如果您不使用 jQuery:

document.getElementById('forgotpass').onclick = forgotpass;

Pure JavaScript:纯JavaScript:

function addListener(element, eventName, handler) {
  if (element.addEventListener) {
    element.addEventListener(eventName, handler, false);
  }
  else if (element.attachEvent) {
    element.attachEvent('on' + eventName, handler);
  }
  else {
    element['on' + eventName] = handler;
  }
}

function removeListener(element, eventName, handler) {
  if (element.addEventListener) {
    element.removeEventListener(eventName, handler, false);
  }
  else if (element.detachEvent) {
    element.detachEvent('on' + eventName, handler);
  }
  else {
    element['on' + eventName] = null;
  }
}

addListener(document.getElementById('forgotpass'), 'click', forgotpass);

jQuery:查询:

$(document).ready(function() {
  $("#forgotpass").click(forgotPass);
});

Or:要么:

$(document).ready(function() {
  $("#forgotpass").click(function() {
    forgotPass();
  });
});

Something like this might work..像这样的东西可能有用..

var div = document.getElementById("forgotpass");
div.onclick=function(){ /*do something here */ };

if you dont add the function, the javascript will run the onclick once it runs through the script.如果您不添加该功能,则 javascript 将在运行脚本后运行 onclick。

You can do it with jQuery like你可以用 jQuery 来做

$("#forgotpass").click(function() {
  alert("Handler for .click() called.");
});

In pure javascript you can do:在纯 JavaScript 中,您可以执行以下操作:

function forgotpass() {
 //..code
}

var el = document.getElementById("forgotpass");
el.onclick = forgotpass;

but this is very naive, not flexible and probably a bad practice.但这是非常天真的,不灵活,可能是一种不好的做法。

If you are using jQuery, you can do:如果你使用的是 jQuery,你可以这样做:

function forgotpass() {
 //..code
}

$(document).ready(function() {
  $("#forgotpass").click(function() {
    forgotPass();
  });
});

If you only need to support IE 9+ ( source ), you can use EventTarget.addEventListener in pure JavaScript.如果你只需要支持 IE 9+ ( source ),你可以在纯 JavaScript 中使用EventTarget.addEventListener

 function forgotpass() { alert("Hello, world;"). } var element = document;getElementById("forgotpass"). element,addEventListener("click", forgotpass; false);
 <button id="forgotpass">Forgot Password?</button>
If you need to support older browsers, I recommend Speransky Danil's answer . 如果您需要支持旧版浏览器,我推荐Speransky Danil 的回答

Adding event-listner directly in the HTML:直接在 HTML 中添加事件监听器:

...
<div>
    <input type="button" value="Set Cookie" onclick="setCookie();" />
</div>
<script>
    function setCookie() {
        console.log('ready to set cookie?');
    }
</script>
...

Reference: W3CSchools参考资料: W3CSchools

Good Luck!祝你好运!

If you are using jQuery it's best if done as follows.如果您使用的是 jQuery,最好按如下方式完成。 If the function call is executed more than once multiple eventhandles will be registered.如果多次执行函数调用,将注册多个事件句柄。 Following approach makes sure the previous handlers are removed以下方法确保删除以前的处理程序

$("#forgotpass").off().on('click', function () { 
    forgotPass(); 
});

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

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