所以也许我只是没有在正确的地方寻找,但我找不到一个很好的解释来说明如何做相当于 jQuery 的操作
So maybe I'm just not looking in the right places but I can't find a good explanation of how to do the equivalent of jQuery's
$('a').click(function(){
// code here
});
在普通的旧 JavaScript 中?
in plain old JavaScript?
基本上我想在每次单击 a 标记时运行一个函数,但我没有能力将 jQuery 加载到页面中以按上述方式执行此操作,所以我需要知道如何使用纯 JavaScript 来做到这一点.
Basically I want to run a function every time an a tag is clicked but I don't have the ability to load jQuery into the page to do it in the way above so I need to know how to do it using plain JavaScript.
工作示例:http://jsfiddle.net/6ZNws/
HTML
<a href="something">CLick Here</a>
<a href="something">CLick Here</a>
<a href="something">CLick Here</a>
Javascript:
Javascript:
var anchors = document.getElementsByTagName('a');
for(var z = 0; z < anchors.length; z++) {
var elem = anchors[z];
elem.onclick = function() {
alert("hello");
return false;
};
}
这篇关于jQuery 的香草 JavaScript 版本 .click的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
在 Angular 2/Typescript 中使用 IScrollUse IScroll in Angular 2 / Typescript(在 Angular 2/Typescript 中使用 IScroll)
Anime.js 在 Ionic 3 项目中不起作用anime.js not working in Ionic 3 project(Anime.js 在 Ionic 3 项目中不起作用)
Ionic 3 - 使用异步数据更新 ObservableIonic 3 - Update Observable with Asynchronous Data(Ionic 3 - 使用异步数据更新 Observable)
Angular 2:在本地 .json 文件中找不到文件Angular 2: file not found on local .json file(Angular 2:在本地 .json 文件中找不到文件)
在 Ionic 2 中,如何创建使用 Ionic 组件的自定义指In Ionic 2, how do I create a custom directive that uses Ionic components?(在 Ionic 2 中,如何创建使用 Ionic 组件的自定义指令?)
将 ViewChild 用于动态元素 - Angular 2 &离子2Use ViewChild for dynamic elements - Angular 2 amp; ionic 2(将 ViewChild 用于动态元素 - Angular 2 amp;离子2)