javascript 如何使用jquery查找图像名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9993773/
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
How to find the image name using jquery?
提问by bala3569
Here in this html i need to get the image name arrow_down using jquery and set it in a variable x.Here in my code after getting the image name i have to use that name in if else condition
在此 html 中,我需要使用 jquery 获取图像名称 arrow_down 并将其设置在变量 x.Here 中,在获取图像名称后,我必须在 if else 条件下使用该名称
<a style="color: #FFFFFF; text-decoration: none;" id="imageDivLink" href="#">
<img name="arrow" class="img-swap" src="Images/arrow_down.jpg" width="13" height="13" border="0" alt="" /></a>
and
和
$('#imageDivLink').click(function () {
var x = $("img[src$='arrow_down.jpg'][name='arrow']");
if (x == arrow_down) {
$('#MainContent_IFTrendAnalysis').animate({ height: '+=120' }, 500);
}
else {
$('#MainContent_IFTrendAnalysis').animate({ height: '-=120' }, 500);
}
});
Any suggestion?
有什么建议吗?
采纳答案by Matt Fellows
use attr("src") to get the src attribute.
使用 attr("src") 获取 src 属性。
$('#imageDivLink').click(function () {
var x = $("img[src$='Images/arrow_down.jpg'][name='arrow']");
if (x.attr("SRC") == "Images/arrow_down.jpg") {
$('#MainContent_IFTrendAnalysis').animate({ height: '+=120' }, 500);
}
else {
$('#MainContent_IFTrendAnalysis').animate({ height: '-=120' }, 500);
}
});
回答by chipcullen
bart s is mostly right - but to complete the jquery:
bart s 大部分是正确的 - 但要完成 jquery:
$('#imageDivLink').on('click',function () {
var imgName = $(this).find('img').attr('name');
if(imgName === 'arrow_down') {
//do something
}
else {
//do something else
}
}
回答by Mark Walters
I would give the image an ID like below
我会给图像一个像下面这样的 ID
<img name="arrow" class="img-swap" id="myImage" src="Images/arrow_down.jpg" width="13" heigh="13" border="0" alt="" /></a>
Then using jquery you can grab the attribute name like this
然后使用jquery,您可以像这样获取属性名称
var imageName = $('#myImage').attr('name');
Hope that helps
希望有帮助
回答by Hiren H Patel
YOu can use the .attr() function provided by jQuery.
您可以使用 jQuery 提供的 .attr() 函数。
You can create custom attribute & the access it in jQuery.
您可以创建自定义属性并在 jQuery 中访问它。
Example
例子
Then in jQuery $().jQuery(function(){
然后在 jQuery $().jQuery(function(){
var v=$('#id').attr('CustomName');
});
});
回答by Srinivasan.S
HTML:
HTML:
<img class="zoom_img" src="images/8.jpg" id="my_img" name="my_img"/>
jQuery:
jQuery:
To get the name by using class name use the below example:
要使用类名获取名称,请使用以下示例:
$('.zoom_img').click(function(e){
var fileName = $(this).attr('src');
var fieldName = $(this).attr('name');
alert('FileName : ' + fileName + '\nFieldName : ' + fieldName);
});
To get the name by using field id use the below example:
要使用字段 id 获取名称,请使用以下示例:
$('#my_img').click(function(e){
var fileName = $('#my_img').attr('src');
var fieldName = $(this).attr('name');
alert('FileName : ' + fileName + '\nFieldName : ' + fieldName);
});
Note:When using field id, we can get the value either by using field id or thiskeyword.
注意:在使用field id 时,我们可以通过field id 或this关键字来获取值。