Javascript 使用jQuery在单击超链接时获取其名称
当您使用jQuery单击超链接时,是否有方法获取超链接名称?我有以下代码,我需要一些jQuery指导:Javascript 使用jQuery在单击超链接时获取其名称,javascript,jquery,html,asp.net,Javascript,Jquery,Html,Asp.net,当您使用jQuery单击超链接时,是否有方法获取超链接名称?我有以下代码,我需要一些jQuery指导: <a href="#" id="imageClick" name='<%# Eval("fileName1") %>'><asp:Image ID="myImage" name='<%# Eval("fileName1") %>' runat="server" ImageUrl='<%#"~/Image/" + Eval("fileName")
<a href="#" id="imageClick" name='<%# Eval("fileName1") %>'><asp:Image ID="myImage" name='<%# Eval("fileName1") %>' runat="server" ImageUrl='<%#"~/Image/" + Eval("fileName") %>' /></a>
当我单击其中一个单选按钮时,该功能可以正常工作。但现在他们又增加了另一项要求,即如果他们单击图像,他们显然希望得到相同的结果,而不会破坏单选按钮的功能。您可以在单击处理程序中使用此.name访问它。这里是Dom元素,不需要jquery来检索元素属性值,所以直接访问元素的name属性即可
$('#imageClick').click(function(){
alert(this.name);
});
编辑
如果单击图像,则不会触发表单更改;与输入、选择、文本区域等不同,因此您需要在图像单击事件上手动触发表单更改,以模拟单选按钮单击触发表单更改事件
将单击处理程序绑定到图像以添加类:
$('yourimageselector').click(function(){
$(this).toggleClass('checkedImage'); // Add a class on first click and again clicked on it remove the class to say it is turned off. If you dont want a turn off functionality simply say :
//$(this).addClass('checkedImage'); //or if this is the only class then this.className = 'checkedImage' classList is not yet supported in all browsers.
$('yourform').change(); //as this is image it wont trigger form change event so you need to manually trigger form's change event (only for input, select, textarea etc form change will be triggered).
});
在您的活动中:
$("#form1").change(function () {
var imgNames= $('.checkedImage')
.map(function(){return this.name; })
.get(); // Will get you all the image names in an array.
//if it is just one image then simply do if($('.checkedImage').length > 0) $('.checkedImage')[0].name,
//Code follows
});
您可以在单击处理程序中使用this.name访问它。这里是Dom元素,不需要jquery来检索元素属性值,所以直接访问元素的name属性即可
$('#imageClick').click(function(){
alert(this.name);
});
编辑
如果单击图像,则不会触发表单更改;与输入、选择、文本区域等不同,因此您需要在图像单击事件上手动触发表单更改,以模拟单选按钮单击触发表单更改事件
将单击处理程序绑定到图像以添加类:
$('yourimageselector').click(function(){
$(this).toggleClass('checkedImage'); // Add a class on first click and again clicked on it remove the class to say it is turned off. If you dont want a turn off functionality simply say :
//$(this).addClass('checkedImage'); //or if this is the only class then this.className = 'checkedImage' classList is not yet supported in all browsers.
$('yourform').change(); //as this is image it wont trigger form change event so you need to manually trigger form's change event (only for input, select, textarea etc form change will be triggered).
});
在您的活动中:
$("#form1").change(function () {
var imgNames= $('.checkedImage')
.map(function(){return this.name; })
.get(); // Will get you all the image names in an array.
//if it is just one image then simply do if($('.checkedImage').length > 0) $('.checkedImage')[0].name,
//Code follows
});
这也可以在单击的事件处理程序中工作:
document.getElementById("new-answer-activity").name
这也可以在单击的事件处理程序中工作:
document.getElementById("new-answer-activity").name
一半是jQuery,一半是JavaScript!那是什么魔法@MohammadAreebSiddiqui您需要确定何时使用jquery以及何时不使用jquery。。。这将回答您的问题。但是,如果您包括jQuery,那么将其作为纯jQuery方法!:OP希望它也是jQuery!:@MohammadAreebSiddiqui的评论与上述相同。当您可以直接访问某些内容时,为什么要使用包装器?我不明白。当this.name给你想要的东西时,我认为做$this.attr'name是愚蠢的。@Moh:仅仅为了使用jQuery而使用jQuery是不明智的。当它真正提供好处时应该使用它,当它只增加开销而没有好处时应该避免使用它。一半是jQuery,一半是JavaScript!那是什么魔法@MohammadAreebSiddiqui您需要确定何时使用jquery以及何时不使用jquery。。。这将回答您的问题。但是,如果您包括jQuery,那么将其作为纯jQuery方法!:OP希望它也是jQuery!:@MohammadAreebSiddiqui的评论与上述相同。当您可以直接访问某些内容时,为什么要使用包装器?我不明白。当this.name给你想要的东西时,我认为做$this.attr'name是愚蠢的。@Moh:仅仅为了使用jQuery而使用jQuery是不明智的。当它实际提供好处时应该使用它,当它只增加开销而没有好处时应该避免使用它。OP需要一种jQuery方法!:OP需要jQuery方法!:好的,所以在表单更改时,你们需要找出哪个图像被点击了,对吗?你们只有一个图像?不,我有数千个。我正在使用我的问题中显示的图像名称动态加载listView。因此,你想知道哪些图像已被单击?好的,那么在表单更改时,你需要确定单击了哪个图像,对吗?你只有一个图像?不,我有数千个。我正在使用问题中显示的图像名称动态加载listView。那么您想知道单击了哪些图像吗?