[英]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.