[英]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.