Jquery:按类或选择器获取链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9869540/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 11:23:49  来源:igfitidea点击:

Jquery: get link by class or selector

jqueryhyperlinkselector

提问by user1292580

I'm creating a list of images from a folder, and then I open them in a fancybox... so I have thumnails, big images, and a text link to full image.

我正在从一个文件夹中创建一个图像列表,然后我在一个fancybox 中打开它们......所以我有缩略图、大图像和一个指向完整图像的文本链接。

<div id="download_image">
  <p><a href="images/download/folder/big/<?php echo $file;?>" class="fancybox"><img src="<?php echo $path;?><?php echo $file?>" /></a></p>
  <p><a href="images/download/folder/full/<?php echo $file;?>.zip" class="btn-download">DOWNLOAD</a></p>
</div>

Then I'm getting images path by jquery, for each image:

然后我通过 jquery 为每个图像获取图像路径:

var download_image = jQuery('#download_image');
var images  = download_image.find('img');

download_images.each(function(){
    var img = jQuery(this);
    var img_source = img.attr('src');

Now I need to get the url of the "btn-download" link for each image... but I don't know which selector must I use! Any advice?

现在我需要获取每个图像的“btn-download”链接的 url ......但我不知道我必须使用哪个选择器!有什么建议吗?

回答by Ben Carey

Use $('.btn-download')to select an element by class.

用于$('.btn-download')按类选择元素。

If you want to get the href attribute then simply use:

如果你想获得 href 属性,那么只需使用:

var href = $('.btn-download').attr('href');

To loop through each element use:

要遍历每个元素,请使用:

$('.btn-download').each(function(){
    var href = $(this).attr('href');
    // do something with href
})

回答by Amir Ismail

Try this

尝试这个

$('a[class=btn-download]').attr('href')

回答by Mathias Bynens

$('.btn-download').each(function() {
  console.log(this.href); // get the normalized `href` property; fastest solution
  console.log($(this).attr('href')); // get the exact `href` attribute value
});

Note that the answers advocating $('.btn-download').attr('href')are incorrect, as those would only return the hrefattribute value for the first element in the set.

请注意,提倡的答案$('.btn-download').attr('href')是不正确的,因为它们只会返回href集合中第一个元素的属性值。

回答by elaxsj

Try this:

尝试这个:

$('#download_image').each(function(){
    var img_source = $('img', this).attr('src');
    var link_href = $('a.btn-download', this).attr('href');
});

回答by Jesse van Assen

var download_image = jQuery('#download_image');

download_image.each(function() {
    var imgUrl = $(this).find('p > a > img').prop('src');
    var downloadUrl = $(this).find('p > a.btn-download').prop('href');
    // do something with the urls here.
});

Sample

样本

The each-function is kind of redundant, because you should only have one element with a specific id. Listed here just in case you want to have more than one download image and button.

each功能全是那种多余的,因为你只能有一个特定ID的一个元素。此处列出以防万一您想要多个下载图像和按钮。

In your question you mention that you are getting the path for each image. I strongly advise you to make download_imagea class if you have more than one download_imageelement.

在您的问题中,您提到您正在获取每个图像的路径。download_image如果您有多个download_image元素,我强烈建议您创建一个类。