简体   繁体   English

如何将此JavaScript事件从内联事件移动到其自己的.JS文件?

[英]How do I move this JavaScript event from an inline event to its own .JS file?

Happened across a snip of JS that I've managed to make work for my uses, but as I'm brand new to javascript, I'm unsure whether I've understood how & why it works. 碰巧遇到了我设法为自己使用的JS片段,但是由于我是javascript的新手,所以我不确定我是否了解它的工作方式和原因。 As it stands, it's inline (inside an html tag, but I'd like to see it either in its own .js file so it can be used by several thousand links on one of several hundred pages on the site I'm putting together. 就目前而言,它是内联的(在html标记内,但我想在自己的.js文件中看到它,以便可以在我整理的网站上数百个页面中的数千个链接中使用它。

The purpose of the javascript I was looking for was to kill or disable a link (and only that link) on a page after it had been clicked once. 我正在寻找的javascript的目的是在单击一次页面后杀死或禁用该页面上的链接(仅该链接)。 I didn't want it to disappear, as I'd seen mention of, just be made un-clickable. 如我所见,我不希望它消失,只是变得不可点击。

Anyway, this is what I found. 无论如何,这就是我发现的。 Assistance is appreciated. 感谢您的协助。

<A HREF="testvid.AVI" onclick="this.onclick=function(){return false;}"><DIV ID="BOX1">This is a video file</DIV></A>

Again, the question is, how to take that onclick event & place it in its own .js file. 同样,问题是,如何进行该onclick事件并将其放置在其自己的.js文件中。 Thank you. 谢谢。

With plain js you can use the same code: 使用纯js,您可以使用相同的代码:

document.getElementById('videoLink').onclick = function() { this.onclick = function() { return false; document.getElementById('videoLink')。onclick = function(){this.onclick = function(){return false; } } }}

With jQuery you can try: 使用jQuery,您可以尝试:

$("#videoLink").one('click', function() {
   $(this).click(function() {
        return false;
   });
});

UPD: UPD:

<A HREF="testvid.AVI" id="videoLink"><DIV ID="BOX1">This is a video file</DIV></A>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script> 
<script type="text/javascript" src="filename.js"></script> 

filename.js filename.js

$(function() {
    $("#videoLink").one('click', function() {
       $(this).click(function() {
            return false;
       });
    });
});

暂无
暂无

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

相关问题 如何将 JavaScript 中的 onclick 事件从内联脚本传输到外部文件? - How do I transfer an onclick event in JavaScript from the inline script to an external file? 如何使用JavaScript从内联onclick事件获取参数,以便在JS文件中使用它们 - How to get parameters from an inline onclick event with javascript, in order to use them inside a JS file 如何替换嵌入式JavaScript事件处理程序? - How do I replace an inline JavaScript event handler? 如何将HTML中的Javascript事件处理程序内联转换为要参数化的? - How do I convert a Javascript event handler inline in HTML to be parameterized? javascript:html内联事件-从其调用获取事件名称 - javascript: html inline events - get the event name from its calling JavaScript内联事件侦听器…它们是否在等待JS加载? - JavaScript inline event listeners…do they wait for JS to load? 如何动态添加具有自己独特的selectahead-on-select事件的AngularJS typeahead组件? - How do I dynamically add a AngularJS typeahead component that has its own unique typeahead-on-select event? 如何在JS外部文件中简化的javascript中使用双击事件 - How do I use a double click event in javascript simplified in a JS external file 如何将此内联 JS 移动到文件中 - How to move this inline JS to a file 如何在Redis中将JS对象传递给它自己的事件参数-Connect事件上的标志 - How to pass JS Object to its own event parameter in redis - sentinal on connect event
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM