简体   繁体   中英

How to find the href path of the anchor tag using jquery?

How can I get and check if the href path contain image path or some other link using jquery. For Example:

<a href="image.png"><img src="image.png"/></a>

In the above example anchor text contain the image src then only jquery function should work to find the href path.ie, Fetch the href path only when anchor text contain img tag.

I reviewed the .has in jquery api for checking the image. But how can i check if the href contain image path or some other path. Something i am going to do based on SO User's Suggestion, like.

<script type="text/javascript">
                            jQuery(document).ready(function($) {
                                if (jQuery('a').has("img")) {  // it return the no. of img tag within a
                                    jQuery('a').has("img").prop('href', '#');
                                }

                            });
                        </script>

See the Above script do the action to check if anchor text contain image tag then only # on href link of anchor tag. By the same way to add like to do the above script to check if the href contain any image path or link. If the Image Path Contain on href then the above script should execute or else it won't.

Any Possible Reason For @Downvoters.?

Is it possible to do in jquery, Any Suggestion would be much appreciated.

you can do

$('a:has(img)').click(function(){
    var href = this.href; //or $(this).attr('href')
    if(/\.(png|gif|jpg|jpeg)$/i.test(href)){
        alert('do')
    }
})

First find the href using attr() jQuery function:

var href = $link.attr('href');

Then you have to verify if the href is an image using the indications from the answers of this question .

Use attr - documentation here.

Example:

My Link

console.log($('#myLink').attr("href"));
var href = $('a').attr('href');

要么

var href = $('a').prop('href');

First you must select the link eg

<a href="link" id="ourlink" ></a>

Then you can use the attr method to get or set href

$("#ourlink").attr('href'); // Here you get it
$("#ourlink").attr('href','anotherlink'); //here you set it

Do somthing like this-

var url = $('a').attr('href');

if(verify url is of type image here){
//true

}
else{
//false

}

Use regexp to check for strings in the href attribute:

var patt=/(png|jpg|gif)/g;
if(patt.test($('a').attr('href'))){
//do something if image
} else {
//do something else
}

You can test the attribute of anchor tag and check using regex that href has a file with extension of an image file.

        //Get the value of href attribute of anchor tag
        var anchorHref = $("a:has(img)").attr("href");

        //Check the extention of the file
        if((/\.(gif|jpg|jpeg|tiff|png|bmp)$/i).test(anchorHref))
        {
            alert("It is Image Path"); 
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM