简体   繁体   English

如何使用jquery查找定位标记的href路径?

[英]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. 如何获取并检查href路径是否包含图像路径或使用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. 在上面的示例中,锚文本包含图像src,然后只有jquery函数才能找到href路径。即,仅当锚文本包含img标签时才获取href路径。

I reviewed the .has in jquery api for checking the image. 我在jquery api中检查了.has以检查图像。 But how can i check if the href contain image path or some other path. 但是我如何检查href是否包含图像路径或其他路径。 Something i am going to do based on SO User's Suggestion, like. 我将根据SO用户的建议执行某项操作,例如。

<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. 请参阅上述脚本,执行操作以检查锚文本是否包含图像标签,然后在锚标签的href链接上仅包含#。 By the same way to add like to do the above script to check if the href contain any image path or link. 用同样的方法添加类似上面的脚本来检查href是否包含任何图像路径或链接。 If the Image Path Contain on href then the above script should execute or else it won't. 如果图像路径包含在href上,则上面的脚本应该执行,否则就不会执行。

Any Possible Reason For @Downvoters.? @Downvoters有任何可能的原因吗?

Is it possible to do in jquery, Any Suggestion would be much appreciated. 是否有可能在jQuery中做,任何建议将不胜感激。

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: 首先使用attr() jQuery函数找到href

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 . 然后,您必须使用此问题的答案中的指示来验证href是否为图片。

Use attr - documentation here. 在此处使用attr 文档。

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 然后,您可以使用attr方法获取或设置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: 使用regexp检查href属性中的字符串:

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. 您可以测试锚标记的属性,并使用正则表达式检查href是否包含带有图像文件扩展名的文件。

        //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"); 
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 <a>使用jquery时</a>抑制锚标记<a>href</a> - Suppress anchor tag <a> href when using jquery 使用 jQuery 选择特定 href 的锚标记 - Selecting the anchor tag of a particular href using jQuery 使用jQuery禁用或更改锚标记的href - disable or change href for an anchor tag using jQuery 如何使用Jquery从锚标记中查找ID - How To Find Id From Anchor Tag Using Jquery 使用jQuery在带有XML内容的锚标记内生成href - Using jQuery to generate a href inside an anchor tag with content from xml 使用href值查找锚标记,并使用新的href值更改href值 - find anchor tag using href value and change href value with new href value 如何在加载后或加载页面时使用 java 脚本或 jquery 从锚标记中删除 href 属性 - How to remove href attribute from anchor tag after loading or while loading the page by using java script or jquery 如何使用jquery将特定类添加到锚标记的href等于URL? - How to add specific class to anchor tag has href equal to URL using jquery? jquery使用div/span ID选择器和锚标签类在锚标签中获取href - jquery to get href in anchor tag using div/span ID selector and anchor tag class 如何将锚标记的 id 附加到 jquery 中的新 href 并替换页面中的 href 属性? - How to append the id of an anchor tag to a new href in jquery and replace the href attribute in a page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM