[英]Can I make a link point to a javascript function?
My code is:我的代码是:
<input class="button" type="button" value="Mark" onClick="doCheck('mark');" \>
I want to make it using an我想用一个
<a>
link.关联。 Is it possible to do this?是否有可能做到这一点? I only know how linking to another page.我只知道如何链接到另一个页面。
Use Like that像这样使用
<a class="button" href="javascript:void(0)" onClick="doCheck('mark');" >Mark</a>
or this way或者这样
<a class="button" href="javascript:doCheck('mark')" >Mark</a>
< a href='javascript:void(null);' onclick='doCheck()' > Test </a>
<a onClick="doCheck('mark');"> Mark </a>
You can attach click handlers to any DOM element that is visible on the page.您可以将点击处理程序附加到页面上可见的任何 DOM 元素。 I would seperately recommend seperation of markup and javascript.我会单独推荐标记和 javascript 的分离。
So something like.所以类似的东西。
<a id="mark">
...
<script type="text/javascript">
$(function() {
$("#mark").click(function() {
doCheck("mark");
});
});
</script>
Would be preferable.会更可取。 In this case $
is jQuery
.在这种情况下$
是jQuery
。 A pure javascript solution is possible but that has a lot of boilerplate code that hides the point.纯 javascript 解决方案是可能的,但它有很多样板代码隐藏了这一点。
This code example will execute the JavaScript without following the link:此代码示例将执行 JavaScript 而无需点击链接:
<a href="#" onclick="doCheck('mark'); return false;">Mark</a>
If you want to follow the link, drop the return false;
如果你想点击链接,请放弃return false;
part:部分:
<a href="somewhere.html" onclick="doCheck('mark');">Mark</a>
You can use the onClick attribute on any html element.您可以在任何 html 元素上使用 onClick 属性。 So use it in your a
tag or in your img
tag.所以在你a
标签或img
标签中使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.