jquery 将 onclick 事件添加到 href 链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1271143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery adding an onclick event to a href link
提问by leora
i am converting over from websforms to asp.net mvc and i have a question.
我正在从 websforms 转换为 asp.net mvc,我有一个问题。
i have a loop that generates links dynamicallly where picNumberLink is a variable in a loop and image is a variable image link.
我有一个动态生成链接的循环,其中 picNumberLink 是循环中的一个变量,而 image 是一个可变的图像链接。
i want to avoid putting javascript actions inline so in my webforms project is did the following:
我想避免将 javascript 操作内联,因此在我的 webforms 项目中执行以下操作:
hyperLink.Attributes.Add("onclick", "javascript:void(viewer.show(" + picNumberlink + "))");
what is the equivalent using jquery in asp.net mvc?
在 asp.net mvc 中使用 jquery 的等价物是什么?
I have seen examples of using the $(document).ready event to attach on clicks but i can't figure out the syntax to pass in the picNumberLink variable into the javascript function.
我已经看到使用 $(document).ready 事件附加点击的示例,但我无法弄清楚将 picNumberLink 变量传递到 javascript 函数的语法。
suggestions?
建议?
回答by karim79
EDIT: If you generate your links with the ID of this form:
编辑:如果您使用此表单的 ID 生成链接:
<a id="piclink_1" class="picLinks">...</a>
<a id="picLink_2" class="picLinks">...</a>
<script type="text/javascript">
$('a.picLinks').click(function () {
//split at the '_' and take the second offset
var picNumber = $(this).attr('id').split('_')[1];
viewer.show(picNumber);
});
</script>
回答by Arnis Lapsa
var functionIWantToCall = function(){
var wrappedLink = $(this);
//some serious action
}
//this should be called on document ready
$("#myLinkId").click(functionIWantToCall);
If you need to get URL of picture, keep it in anchor`s href:
如果您需要获取图片的 URL,请将其保存在 anchor`s href 中:
var functionIWantToCall = function(event){
event.preventDefault(); //this one is important
var link = $(this).attr('href');
//some serious action
}
回答by Rigobert Song
$(document).ready(function()
{
$('#LinkID').click(function() {
viewer.show($(this).attr('picNumber'));
});
});
You can add an attribute called picNumber to your hyperlink tag and set this is your mvc view
您可以将名为 picNumber 的属性添加到您的超链接标签,并将其设置为您的 mvc 视图
The link in your view might look something like this:
您视图中的链接可能如下所示:
<%= Html.ActionLink("Index", new { id = "LINKID", picNumber = 1 }) %>
回答by searlea
Assuming you're able to change the HTML you output, can't you put the picNumberLink in the id
or class
attribute?
假设您能够更改输出的 HTML,您不能将 picNumberLink 放在id
orclass
属性中吗?
HTML:
HTML:
<a href="..." id="piclink-242" class="view foo-242"><img src="..."/></a>
jQuery:
jQuery:
$(function() {
// using the id attribute:
$('.view').click(function() {
viewer.show(+/-(\d+)/.exec(this.id)[1]);
});
// or using the class attribute:
$('.view').click(function() {
viewer.show(+/(^|\s)foo-(\d+)(\s|$)/.exec(this.className)[2]);
});
}}